Custom icon for specific file type in Nautilus on Ubuntu 22.04

  • Thread starter Wrichik Basu
  • Start date
In summary, Nautilus on Ubuntu 22.04 allows users to customize the icon for specific file types. This feature allows for easier identification and organization of different file types within the file manager. Users can choose from a variety of pre-installed icons or upload their own custom icons to represent specific file types. This customization option adds a personal touch to the file management experience on Ubuntu 22.04.
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,138
2,713
My system is Ubuntu 22.04.4; GNOME Shell v42.9

The application is HomeBank, installed via flatpak. The files have an extension .xhb. I want to associate the icon of this application to this file type.

I found the application logo files under /usr/share/icons/hicolor/ZxZ/apps/homebank.png. The available sizes (Z) are 16, 22, 24, 32, 48, 256. There was no scalable .svg icon, but I have built one.

A Google search brought up this answer from AskUbuntu. I'll list what I did step by step. The OP wanted a custom icon for application/x-hwp files. I want it for application/x-homebank files.
1. Edited /etc/mime-types to add the line:
Code:
application/x-homebank        xhb

I confirmed the MIME type by right-clicking on a .xhb file → Properties in Nautilus.

2. Created an mimetypes/ directory under /usr/share/icons/gnome/scalable/.

3. Copied the scalable icon as below:
Bash:
sudo cp /path/to/icon/application-x-homebank.svg /usr/share/icons/gnome/scalable/mimetypes/

4. Restarted GNOME with Alt + F2r.
Icons did not change in nautilus.

Found another answer to a similar question on AskUbuntu again. The OP wanted to add custom icons for MATLAB files and figures. Here's what I did:
1. Created the following XML file:
~/homebank.xml:
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  <mime-type type="application/x-homebank">
    <comment>Homebank data file</comment>
    <glob pattern="*.xhb"/>
    <icon name="application-x-homebank"/>
  </mime-type>
</mime-info>

This XML structure is also shown in the docs of xdg-mime.

2. Ran the following:
Bash:
sudo xdg-mime install --novendor ./homebank.xml

This added the following file in /usr/share/mime/application/x-homebank.xml:
x-homebank.xml:
<?xml version="1.0" encoding="utf-8"?>
<mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info" type="application/x-homebank">
  <!--Created automatically by update-mime-database. DO NOT EDIT!-->
  <comment>Homebank data file</comment>
  <glob pattern="*.xhb"/>
  <icon name="application-x-homebank"/>
</mime-type>

That probably shows that xdg-mime worked fine.

3. Painstakingly installed the icons, for each of the available sizes listed above, as follows:
Bash:
sudo xdg-icon-resource install --novendor --noupdate --context mimetypes --size 48 homebank_48.png application-x-homebank

4. The above step also created symlinks. The directory structure of /usr/local/share/icons/hicolor/ is like this:

/usr/local/share/icons/hicolor tree:
├── 16x16
│   ├── apps
│   │   └── ...
│   └── mimetypes
│       ├── application-x-homebank.png
│       └── gnome-mime-application-x-homebank.png -> application-x-homebank.png
├── 22x22
│   └── mimetypes
│       ├── application-x-homebank.png
│       └── gnome-mime-application-x-homebank.png -> application-x-homebank.png
├── 24x24
│   ├── apps
│   │   └── ...
│   └── mimetypes
│       ├── application-x-homebank.png
│       └── gnome-mime-application-x-homebank.png -> application-x-homebank.png
├── 256x256
│   ├── apps
│   │   ├── ...
│   └── mimetypes
│       ├── application-x-homebank.png
│       └── gnome-mime-application-x-homebank.png -> application-x-homebank.png
├── 32x32
│   ├── apps
│   │   └── ...
│   └── mimetypes
├── 48x48
│   ├── apps
│   │   └── ...
│   └── mimetypes
│       ├── application-x-homebank.png
│       └── gnome-mime-application-x-homebank.png -> application-x-homebank.png
└── scalable
    └── mimetypes
        ├── application-x-homebank.svg
        └── gnome-mime-application-x-homebank.svg -> ./application-x-homebank.svg

The last directory scalable/ was added by me manually because xdg-icon-resource doesn't support scalable icons by default. I did this with:
Bash:
sudo mkdir scalable
cd scalable
sudo mkdir mimetypes
cd mimetypes
sudo cp ~/path/to/scalable_icon.svg ./application-x-homebank
sudo ln -s ./application-x-homebank.svg ./gnome-mime-application-x-homebank.svg

5. Then ran
Bash:
sudo xdg-icon-resource forceupdate

6. Restarted Gnome shell.

Nothing. Absolutely no change.

Came across this question on Unix-Linux SE, decided to give it a go. Ran the following:
Bash:
update-mime-database ~/.local/share/mime
sudo update-mime-database /usr/share/mime/
update-icon-caches ~/.local/share/icons
sudo update-icon-caches /usr/share/icons/
sudo update-icon-caches /usr/local/share/icons/
And, you probably guessed correctly — still nothing.

I am at a loss at this point.
 
Last edited:
Computer science news on Phys.org
  • #2
Official documentation (assuming the MIME type text/x-python py is already in /etc/mime.types):
https://help.ubuntu.com/community/AddingMimeTypes said:

Adding an Icon​


Now we need to associate an icon with the MIME type. Get an SVG icon and name it "text-extension.svg", or whatever your modified MIME type is named; this will be the icon to represent all instances of the MIME type on your system. For our python example, this is a good choice. Rename the .svg file so that the it matches "text-x-python.svg" (or "insertYourMIMEtype.svg") so that the slashes are replaced with "-" and there are no capital letters.

Then simply run the following commands, with 'text-x-python' replaced with your MIME type.

Code:
sudo cp text-x-python.svg /usr/share/icons/gnome/scalable/mimetypes
sudo gtk-update-icon-cache /usr/share/icons/gnome/ -f

Relogin and all files ending in the MIME extension will display with that icon.
Note that the cache was updated AND you have to logout and relogin.
 
  • #3
jack action said:
Official documentation (assuming the MIME type text/x-python py is already in /etc/mime.types):

Note that the cache was updated AND you have to logout and relogin.
Did that just now. (MIME type was already added to /etc/mime.types)
Bash:
sudo cp ./application-x-homebank.svg /usr/share/icons/gnome/scalable/mimetypes/
sudo gtk-update-icon-cache /usr/share/icons/gnome/ -f

Still nothing. 😭
 
  • #4
In your first solution, there was also the following comment:
https://askubuntu.com/questions/52138/how-do-i-change-the-icon-for-a-particular-file-type/56725#comment1709925_56725 said:
You also need to check ~/.local. For example ~/.local/share, which mimics the folder /usr/local/share. Your locally installed application (i.e. application for you only, not other logins), may have their properties there. Edit similar files, for example, ~/.local/share/icons/XXXX/XXXX
I know that Chrome added some images on my system in ~/.local/share/icons/hicolor/.
 
  • #5
jack action said:
In your first solution, there was also the following comment:

I know that Chrome added some images on my system in ~/.local/share/icons/hicolor/.
Executing ls -R ~/.local/ | grep homebank returns nothing. Should I put the icon files inside the respective directories in .local too?
 
  • #6
I've been playing around for a while now trying to change the Python icon on my system and I cannot do it as well. I will follow this thread.
 
  • #7
Sounds like fun, I'll have a go too. Quick question:
Wrichik Basu said:
There was no scalable .svg icon, but I have built one.
Are you sure this is 'proper' SVG and not a bitmap embedded in SVG markup? Have you tried first using some other icon you know works in Nautilus?
 
  • #8
pbuk said:
Are you sure this is 'proper' SVG and not a bitmap embedded in SVG markup? Have you tried first using some other icon you know works in Nautilus?
Just to rule out this possibility, I copied the SVG from /usr/share/icons/hicolor/apps/scalable/audacity.svg to all the locations where I had copied the homebank icon. The SVG for Audacity was placed by flatpak, so it should be fine. Updated all icon-caches, logged out and back in. The issue persists.
 
  • Like
Likes pbuk
  • #9
I haven't made much progress, been having problems with VirtualBox v7 and Ubuntu.

You do realise that the SO thread you are following is more than 10 years old? Gnome has changed a lot in that time.
 
  • #10
pbuk said:
You do realise that the SO thread you are following is more than 10 years old? Gnome has changed a lot in that time.
Yeah I know, but there isn't anything more recent available on this. At least I didn't find it via Google.
 
  • #11
So in addition to posting here, I had also posted on GNOME's community help forum. One person told me that this might be because of the theme. So I opened the gnome-tweaks-tool and changed the icon theme to Gnome, and behold! The file icons are precisely what I wanted!

I had earlier used the scalable icon from Audacity to remove any issues with the SVG file itself. I copied the proper SVG to /usr/share/icons/gnome/scalable/mimetypes/application-x-homebank.svg, then logged out and back in. And it worked. The icons for that specific file type have changed.

Edit: The interesting part is that, if I choose Hicolor in the Tweaks tool, the icon in /usr/share/icons/hicolor/scalable/mimetypes/application-x-homebank.svg is activated (verified this by copying the audacity scalable icon as the homebank icon).

Edit 2: The icon theme that I use is Yaru-blue-dark. Quite obviously, creating an icon in /usr/share/icons/Yaru-blue-dark/scalable/mimetypes/application-x-homebank.svg fixed the issue; the files with .xhb extension now have the icon that I would like them to have.
 
Last edited:

Similar threads

Back
Top