Search code examples
linuxbashgnomexdgutils

Copying all .desktop files to ~/.local/share/applications/


I'm trying to write a bash script to copy all .desktop files under /nix/store to ~/.local/share/applications/. I'm not particular good with bash, so I would like assistance. I used the find command to find all files. Now I'm trying to create an array from the output with the help of readarray:

files=$(find /nix/store -type f -name \*.desktop)
echo $files
x=$(readarray -d 's'  <<<$files)
echo $x

The echo $files will print the result of the find command, however the echo $x prints an empty line.


Solution

  • #!/usr/bin/env bash
    
    files=$(find /nix/store -type f -name \*.desktop)
    
    readarray array <<<$files
    
    for i in ${array[@]}; do
        cp $i ~/.loca/share/applications/
    done
    

    or

    find /nix/store -type f -name \*.desktop -exec cp {} ~/.local/share/applications/ \;