'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 1:[1]
#!/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/ \;
Solution 2:[2]
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
Solution 3:[3]
If I'm not wrong and /nix/store/ is a regular directory, this should work:
cp /nix/store/*.desktop ~/.local/share/applications/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | KamilCuk |
| Solution 3 | Mirsen Kahrovic |
