Quantcast
Channel: Copy the same file to the desktops of all users - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by Kusalananda for Copy the same file to the desktops of all users

$
0
0

Since cp only ever takes a single destination name, you would have to make it a loop and call copy the files individually:

for destdir in /home/*/Desktop/; do
    cp myfile "$destdir"
done

Using xargs here, as you suggest, would probably work, but would be cumbersome:

printf '%s\n' /home/*/Desktop/ | xargs -I {} cp myfile {}

This relies on the names of the subdirectories under /home not having newlines in them (but I know of no Unix that allows newlines in usernames).

It is conceivable that you'd also like to change the owner of the file to that of the owner of the corresponding Desktop directory.

You can do this conveniently with GNU chown (from coreutils) like so:

for destdir in /home/*/Desktop/; do
    cp myfile "$destdir" &&
    chown --reference="$destdir" "$destdir/myfile"
done

Doing that with xargs:

printf '%s\n' /home/*/Desktop/ | xargs -I {} sh -c 'cp myfile "$1" && chown --reference="$1" "$1/myfile"' sh {}

Viewing all articles
Browse latest Browse all 3

Trending Articles