📅 June 12, 2018
You are probably aware that you can set a custom icon using the file manager’s GUI, but did you know that you can also set a custom icon on a folder using the command line?
That’s right. The gvfs-set-attribute command will allow you to set a custom icon to a directory so that the new icon will appear in place of the default folder icon when viewed in a GUI file manager.
This is particularly handy for assigning multiple custom icons for movie and music directories while preserving the default folder icons for other system directories. It can be scripted, and it is easier to do than you think.
The command gvfs-set-attribute allows us to change various attributes related to a file or directory. Suppose we have a directory named test with the following directories as shown.
All directories share the same default folder icon. We want to set a custom icon to the apple directory.
We can use gvfs-info to view file/directory attribute information. Enter man gvfs-info to see what is possible. Here is an example:
To view which attributes are writable, use the -w option.
gvfs-info -w apple
Shown above are the attributes we can set for the apple directory. The attribute we want is named metadata, which is writable. The full attribute is metadata::custom-icon (highlighted in yellow in the image). Setting this attribute will point to whatever image file we specify as the new folder icon. (The ::custom-icon part was located after research and reading online.)
Set the Custom Icon
We write a string type to the attribute as shown:
gvfs-set-attribute apple -t string metadata::custom-icon file:///absolute/path/to/icon/tux.png
- gvfs-set-attribute …the command itself
- -t string …writing a string data type
- metadata::custom-icon …what attribute are we writing?
- file:///absolute/path/to/icon/tux.png …the argument for the metadata::custom-icon attribute. This must be an absolute path to the image file.
If we run gvfs-info again, we will see the new attribute in the listing.
Did you notice the message “metadata (string, Copy with file, Keep with file when moved)” when running gvfs-info -w apple? If we move the apple directory to another location, the custom icon will be preserved.
Unset the Custom Icon (Return to Default)
Suppose you want to return to the default icon. How can we do this? Easy! Use unset instead of an image path as the argument for the string type.
gvfs-set-attribute apple -t string unset
No Icon Appearing
The custom icon may exist anywhere on your Linux system, so double check that its path is correct or else a generic file icon will appear.
“Why set custom folder icons from the command line?”
Scripting. Plain and simple. While this example focused on a single directory, you can write a Bash script that analyzes a large list of subdirectories and sets a unique custom icon for each using the first image it locates in each subdirectory. This is immensely useful for movie, music, game, picture, and video collections. Combined with Imagemagick, you can modify your script to resize, layer, and compose custom icons dynamically in order to emulate DVD covers or movie posters.