0

To copy .desktop file across all users desktops i used
ls -1 /home/ | while read line ; do cp ~/baz.desktop /home/$line/baz.desktop ; done
I have a feeling though that there is more elegant way to achieve this.

Cheers,
Xi

xi100f
  • 133
  • 4
    Maybe the better place would be /usr/share/applications/ if you want have this application available for all users. – mariaczi Jul 10 '18 at 14:37

2 Answers2

3

Depending on how savvy your users are, you may want to have them copy the file instead of forcing the file onto their accounts, or you may want to install it centrally as suggested by mariaczi in comments.


The file needs to be copied to each user's home directory, if I understand it correctly. And I'm assuming you are doing it as root. After the operation has finished, I'm assuming that the copies not only has to reside in the home directory of each user, but that it should also be owned by that user and belong to the group users with permissions 0644.

This can be done by install in a loop (assuming the home directories are located under /home). The install utility works somewhat like cp but allows for setting user and group ownership as well as permissions in one go.

for homedir in /home/*/; do
    user=${homedir%/}   # remove '/' from end of $homedir
    user=${user#/home}  # remove '/home' from start of $user
    install -b -o "$user" -g users -m 644 ~/baz.desktop "$homedir"
done

install -b will create a backup of the file on the destination if it already exists. Alternatively, you could skip installing the file completely if it already exists:

for homedir in /home/*/; do
    if [ ! -e "$homedir/baz.desktop" ]; then
        user=${homedir%/}   # remove '/' from end of $homedir
        user=${user#/home}  # remove '/home' from start of $user
        install -o "$user" -g users -m 644 ~/baz.desktop "$homedir"
    fi
done

Related:

Kusalananda
  • 333,661
1

As suggested in the comment, you can install the shortcut globally in the system by copying it to /usr/local/share/applications/. If in some reason you still prefer to copy it to the home directory of every user, here is the elegant way you are looking for:

$ ls -1 /home/ | xargs -rI{} sudo cp ~/baz.desktop /home/{}/

Explanation

ls -1 /home/ selects all users' home directories listing them by one in a line.

xargs -rI{} executes the following command separately for each entry on standard input and substitutes {} with the text of the entry (user's directory in our case).

cp ~/baz.desktop /home/{}/ copies your ~/baz.desktop to the home directory of the user.

Trudy
  • 854