How to add an icon for a file type .m (or any specific extension; Ubuntu 20.04)

I am working with MATLAB most of my time and I'd be happy to visually differ code files (.m), data files (.mat), figures (.fig). Quite an obvious thing that turns to be pretty peculiar.

Here I am now:

  • added new mime entry application/matlab m mat fig in /etc/mime.types
  • icons matlab-m.png, matlab-fig.png, matlab-mat.png are put into /usr/share/icons/gnome/8x8...512x512/mimetypes (each folder correct size) and .svg with same names to /usr/share/icons/gnome/scalable/mimetypes
  • put XML named matlab.xml in /usr/share/mime/packages

Here is my .xml:

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  <mime-type type="application/matlab-m">
    <comment>Matlab source code</comment>
    <glob pattern="*.m"/>
    <icon name="matlab-m"/>
  </mime-type>
  <mime-type type="application/matlab-fig">
    <comment>Matlab figure</comment>
    <glob pattern="*.fig"/>
    <icon name="matlab-fig"/>
  </mime-type>
  <mime-type type="application/matlab-mat">
    <comment>matlab data</comment>
    <glob pattern="*.mat"/>
    <icon name="matlab-mat"/>
  </mime-type>
</mime-info>

These actions didn't make system to read icons for .m and .fig files, but suddenly icon for .mat files is detected. Please show me where am I wrong.


Ok, it appeared, that assigning icons for filetypes in Ubunty is a trickier task than one could think. Anyway, I've found a pretty clear explanation in case someone with the same desire finds this page. In short:

A. Create a definition for an unknown filetype:

  1. If your file has uncommon type, so that Ubuntu has problems with detecting / defining it correctly, you need to do it yourself. Such identification is called «MIME type». The MIME type is specified as an .xml document. Here's my example (data file used by Matlab):

    MATLAB DATA FILE
  2. Put .xml file with MIME type to /usr/share/mime/packages — all users ~/.local/share/mime/packages — one user

  3. Update database of MIME types: sudo update-mime-database /usr/share/mime In case of success, a new folder matlab-types was created at /usr/share/mime, containing an auto-generated file matlabdata.xml. Note how it was done according to the type="matlab-types/matlabdata" from our custom MIME type definition. In case I will need to create other Matlab-related MIME types, I will add another <mime-type> ... </mime-type> section in my .xml and the related files will be stored in the same /usr/share/mime/matlab-types directory.

B. Set an icon for a (custom) MIME type:

  1. Rename your icon according to type="matlab-types/matlabdata" from .xml file, but replace '/' with '-': "custom-types-matlabdata.svg"

  2. Put it to the folder, where icons related to your desktop theme are stored:

    sudo cp icon.svg /usr/share/icons/{THEME}/scalable/mimetypes/custom-types-matlabdata.svg

Note, that you can put vector images to …/scalable/… folder. If you have raster .png icons you have to create a set of copies with different scale and put them to their respective folders:

/usr/share/icons/{THEME}/(8x8, 16x16, 22x22…)/mimetypes/…
  1. Update icon cashe (relogin/restart not required):

    sudo update-icon-caches /usr/share/icons/*

  2. DONE!

PS. A little helping code to manipulate icons: put in .sh file and run with sudo

#!/bin/bash

copyName="(NAME).svg"
pasteName="(CATEGORY DIR)-(MIME TYPE).svg"
themeName="(THEME)"
copyFrom='(WHERE DOWNLOADED ICON IS)'
iconsDir="/usr/share/icons" # "~/.icons"
copyTo="$iconsDir/$themeName/scalable/mimetypes"

cp "$copyFrom/$copyName" "$copyTo/$pasteName" && \
echo "FROM:\n\t$copyName\n\t$storagedir\n\nTO:\n\t$pasteName\n\t$pasteTo\n\n"

update-mime-database /usr/share/mime
gtk-update-icon-cache $iconsDir/$themeName/ -f