For a single directory
To set the icon of one file or directory use the following command
gvfs-set-attribute -t string <PATH> metadata::custom-icon <IMAGE-URL>
where <PATH>
is the path to the file or directory and <IMAGE-URI>
is a URI to an image file to use as the icon. To use a local image file use the file:
scheme, e. g. file:///home/sundar/my-icon.png
.
For multiple directories with the same icon
Now to apply this to multiple files or directories you can use a loop command to run the above program multiple times:
for p in <PATHS...>; do
gvfs-set-attribute -t string "$p" metadata::custom-icon <IMAGE-URL>
done
Because it's a little cumbersome to type the whole command every time you want to assign some icons you can create a shell script and save it to a file, e. g. ~/.local/bin/set-custom-icon.sh
:
#!/bin/sh
set -eu
case "$1" in
*://*) icon="$1";;
/*) icon="file://$1";;
*) icon="file://$(readlink -e -- "$1")";;
esac
shift
for p; do
gvfs-set-attribute -t string "$p" metadata::custom-icon "$icon"
done
Set the execute permission on the file
chmod +x ~/.local/bin/set-custom-icon.sh
Now you can use the script like this:
~/.local/bin/set-custom-icon.sh <IMAGE> <PATHS...>
For multiple directories with different, automatically selected icons
We can extend this command to select a different icon file for each directory since there's a simple pattern between the two.
for p in <PATHS...>; do
icon="$(find "$p" -mindepth 1 -maxdepth 1 \( -iname 'album*' -o -iname 'cover*' \) -a \( -iname '*.jp[eg]' -o -iname '*.jpeg' -o -iname '*.png' \) -type f -print -quit)"
[ -z "$icon" ] || gvfs-set-attribute -t string "$p" metadata::custom-icon "file://$(readlink -e -- "$icon")"
done
For each path $p
this will find a regular file directly below $p
whose name begins with album
or cover
and ends with .jpg
, .jpe
, .jpeg
, or .png
(all case-insensitive) if such a file exists. If multiple files matching this pattern exist an arbitrary one is chosen.
If such a file exists it is set as the icon for directory $p
.
For multiple directories with different, manually selected icons
We can do that with a script that opens a file dialogue to ask for an icon location repeatedly and without further user interaction:
for p in <PATHS...>; do
icon="$(zenity --file-selection --title="Select icon for $p" --file-filter='Supported images | *.jp[eg] *.jpeg *.png *.gif *.svg *.xpm' --file-filter='All files | *')" || break
gvfs-set-attribute -t string "$p" metadata::custom-icon "$icon"
done
(requires the zenity
package)