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

313 views Asked by At

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.

3

There are 3 answers

0
red On BEST ANSWER
#!/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/ \;
0
KamilCuk On

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

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

however the echo $x prints an empty line.

readarray produces no output. Instead, it stores the lines in the argument, which represents the name of the variable, the name of the array.

readarray -d 's'  <<<$files
#            ^^^  - stores output in array named s
printf "%s\n" "${s[@]}"  # you can print the array s
0
Mirsen Kahrovic On

If I'm not wrong and /nix/store/ is a regular directory, this should work:

cp /nix/store/*.desktop ~/.local/share/applications/