Set Custom Folder Icon Using Bash

📅 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.

Nemo (Linux Mint 18.3) showing the contents of the test directory. Contains five directories.

All directories share the same default folder icon. We want to set a custom icon to the apple directory.

This is the custom Tux icon we will apply to the apple directory. Located in icons.

Viewing Info

We can use gvfs-info to view file/directory attribute information. Enter man gvfs-info to see what is possible. Here is an example:

gvfs-info apple

This shows the current attribute information for the apple directory.

To view which attributes are writable, use the -w option.

gvfs-info -w apple

Which attributes can we set or write? gvfs-info -w will reveal all.

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.

Now, the apple directory uses the custom icon. The other directories continue to use the default folder icon. (Changes should apply immediately. If not, you might have to log out and then log back in. No need to restart the system.)

If we run gvfs-info again, we will see the new attribute in the listing.

metadata::custom with the image file path now appears at the bottom of the attribute listing for the apple directory. (Image edited.)

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

The icon now returns to normal.

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.

Oops! Tried to use a relative path instead of an absolute path. In addition, the path was misspelled. Simply unset it or retry using a valid absolute path to the custom image.

“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.

Have fun!

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: