Change Freedesktop file icon

Robert Crowther Jan 2022
Last Modified: Feb 2023

For the purpose of a desktop, files are given types. The type is described by a mime string, much the same as a web page has a mime. You know how it works—you see a ‘Text‐file’ icon, you can click on it, it launches the the file in a text editor/word processor.

This article is about how to change the icon associated with a type of file. Elsewhere is another article about how to create a new mime type.

Howto change the icon associated with a mime

The idea is you name your icon after the mime type e.g. ‘text‐x‐garbage.svg’. You dump this specifically‐named icon in one of the icon folders, then run a cache update (yes, icon data needs to be cached). The system then associates the files with the mime type to the new icon.

I recommend…

Get an icon

Make or steal an ‘svg’ icon. If making a new icon, make something simple at 32x32 or 64x64 pixels. Leave a bit of space round the image, and lean towards gray scales, not full‐range contrast. You can always make things better when you know it works.

Find and choose your XDG path

Run this to find configured Freedesktop paths,

$ printenv XDG_DATA_DIRS

You will see several paths. I recommend you configure at a /usr relative path—see the notes below.

If you want to recognise a custom kind of file which this icon will identify

You need to create a new mime type.

Find the main theme folder

Use the GUI and look up the icon theme name in ‘preferences’ or wherever. My theme is ‘Mint‐Y’.

With the path found above, plus the subfolder ‘icons’ and the theme name, you can find the configuration folder for your main theme. Mine is,

/usr/share/icons/Mint-Y/

Now find the …/scalable/mimetypes/ directory (SVG icons are classed as ‘scalable’). If you can’t find one, see the next section,

/usr/share/icons/Mint-Y/mimetypes/scalable/mimetypes

Dump your ‘svg’ file in there.

Configuring a new theme folder

The above will not work in Linux Mint because there is no …/scalable/mimetypes/ folder. The Mint theme folder is organised upside down to the ‘hicolor’ and ‘gnome’ themes, grouping by type then by size. To top that off, there is no ‘scalable’ folder at all.

There are other ways to do this, but I recommend to configure a new directory in the main theme. Create,

mkdir /usr/share/icons/Mint-Y/scalable/mimetypes

Now edit the index.theme file to tell the cache programs to look in the new directory,

nano /usr/share/icons/Mint-Y/index.theme

Add the path to the directory list,

Directories=scalable/mimetypes,...

Then tell the config about what it finds in that folder. Put this at the end of the ‘index.theme’ file,

###################

[scalable/mimetypes]
Size=16
Context=Mimetypes
Type=Scalable
MinSize=8
MaxSize=512

Update cache

Now run this (for GTK. There is a KDE equivalent etc. All these utilities are based in Freedesktop code),

gtk-update-icon-cache /path/to/the/theme/folder

Sadly, this tool has no –verbose option. How we need that! However, that should do it. Your icon will now be associated with all files of the same theme. In Mint/‘nemo’ it updated without a browser refresh. Phew.

Icon advice

Can you do this in userspace?

Yes, a few ways. One way is if the machine has a userspace‐configured mime type. Then you can add the icon‐type as a parameter to the mime configuration.

The reason I do not expand on this is because you may not have a user‐configuration path for the Desktop configuration. Also, icon choosing depends on a theme inheritance chain, so an override is complex. I recommend dumping in your main theme—not because it is the best solution, but because it is reliable.

Placing icons in sub‐theme folders has failed for me and others

Freedesktop configuration lets you inherit one theme from another. There is a line in configuration for inheritance. Here’s the configuration in Mint‐Y theme,

Inherits=Adwaita,gnome,hicolor

Freedesktop recommends placing in the ‘hicolor’ theme, which should always be present. Makes sense right, place your icon in a guaranteed place? Moreover, you would want your icon to be in the base theme folder. That way, it will work even if the theme is changed.

However, this doesn’t work for me, and web posts suggest it has not worked for others. I placed the new icon in every inherited theme and it didn’t work in any of them. I tried adapting sizes to the config. I tried adapting configs. I checked permissions. The Adwaita theme has an ominous comment,

Comment=The Only One

I tried multiple cache‐updates. Nothing worked, only placement in the main theme folder.

I admit, I don’t know what is going on, and since I haven’t stumbled on the answer, I’m not looking for it. Something is wrong. I recommend using the main theme folder.

Why SVG?

Because the Freedesktop system is sophisticated. As you see in the config above, it can snap from one size of icon to another. So drop a PNG into a theme folder, and you will never know if it has associated, you may be viewing at a wrong size. Whereas an SVG will likely to be configured for most sizes. I havn’t checked, but some themes act as if, for scalable icons, the base size field is ignored. If you look at the scalable Mint configuration recommended above, it stretches from 8x8 to 512x512 in size. This configuration was lifted from other themes and will work on most desktop screens. At least, if the association works, you will see something. Whereas, if you are struggling to get anything to show, fixed sizes such as PNG will multiply the issues.

Why is this job difficult?

Good question. You may like to consider that Linux Desktops have spawned project after project to change icons, and no solution has lasted long.

References

Freedesktop icon theme spec,

https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html

Ubuntu answers,

https://askubuntu.com/questions/52138/how-do-i-change-the-icon-for-a-particular-file-type

Freedesktop icon naming spec(useful for a quick check on what is supposed to be standard),

https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html

Archived page on how to make tango icons (still good recommendations and the base of many Linux desktop themes),

https://web.archive.org/web/20160202102503/http://tango.freedesktop.org/Tango_Icon_Theme_Guidelines