2021-05-09 - By Robert Elder
In this article, I will discuss the steps that I took to successfully create a new MIME type association in Ubuntu 20.04 that would match any files that end with the '*.kdenlive' file extension. This article complements a related topic, Adding A Custom 'Open With' Program In Ubuntu 20.04.
By creating this new MIME type and installing it, other programs on your system will be able to automatically identify files of this type and do special things with them (like showing a specific icon, or opening with a specific program). If we run the 'mimetype' command on our project file before installing the MIME type:
we'll get the following result:
but after the MIME type is installed, we'll get this result:
Step 1) Identify The MIME 'Packages' Directory
The first step is to identify the location of the 'packages' directory where you need to put your MIME type definition. In my case, I'm installing this MIME type globally so it's accessible to all users, so I'll use the directory '/usr/share/mime/packages/'. If you're installing an MIME type for a specific user, you might want to use '/home/THE_USER/.local/share/mime/packages/'. If you're on a different system, you might want to read this article on the MIME Database. In the article, they state that MIME types are found in the directories found in the environment variable '$XDG_DATA_HOME' with 'mime' appended to the end of the path of each. Just for fun, if I run this command:
echo "$XDG_DATA_DIRS" | tr ':' '\n'
I'll see a listing of all those directories:
/usr/share/ubuntu /usr/local/share/ /usr/share/ /var/lib/snapd/desktop
As previously noted, the one I'm going to use is '/usr/share'. After appending 'mime' and 'packages' to the path, I get '/usr/share/mime/packages/'.
Step 2) Create The MIME Type Definition
Now, let's create the actual new MIME type(s) in the file 'roberts-custom-mime-types.xml':
sudo vi /usr/share/mime/packages/roberts-custom-mime-types.xml
And here is the actual MIME type(s) description:
<?xml version="1.0" encoding="UTF-8"?> <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'> <mime-type type="roberts-custom-mime-types/custom-kdenlive-project-file-mime-type"> <comment>Robert Elder's Custom Kdenlive Project MIME Type</comment> <glob pattern="*.kdenlive"/> </mime-type> </mime-info>
You can also add instructions to identify files based on the type of contents they have as opposed to just by the file extension. These are often called 'magic values' and you can read more about how to do this GNOME Library Help - Adding MIME types.
Take note that the contents of 'type=' is actually two parts separated by a '/' character. The first part is 'roberts-custom-mime-types' and the second part is 'custom-kdenlive-project-file-mime-type'. The first part is the same as the filename of 'roberts-custom-mime-types.xml'. That's important as we'll see in the next section.
Step 3) update MIME Database
# The location '/usr/share/mime' should correspond to the path in 'XDG_DATA_HOME' that you chose previously. sudo update-mime-database /usr/share/mime
If this worked, it should auto-generate a file at '/usr/share/mime/roberts-custom-mime-types/custom-kdenlive-project-file-mime-type.xml'. If you decide to add additional MIME types to your 'roberts-custom-mime-types.xml' file and then run 'update-mime-database' again, they should all show up in this corresponding 'roberts-custom-mime-types' directory.
Now it should be possible to immediately use commands like 'mimetype' to query for your new MIME type (in my case, no relog or restart was necessary):
will output the following:
We can also query the MIME type using the 'xdg-mime' command:
xdg-mime query filetype proj1.kdenlive
which gives this output:
But, if we try using the file command:
we get this result:
proj1.kdenlive: XML 1.0 document text
which isn't what we want! It turns out, that there's another file that keeps track of file extension/MIME type associations in '/etc/mime.types'. The 'file' command uses this file in determining MIME type, but it also uses a completely separate database (at '/usr/share/file/magic') for finding 'magic' values too! You can read more at debian MIME, and also by checking the 'man' page on 'magic':
For my use case, I didn't bother adding the MIME type support for the file command and nothing bad happened. Doing all the steps above was enough to make the MIME association work through the graphical file manager.
Custom Icon For Custom MIME Type
Here is a simple SVG Icon:
Here is the literal SVG XML that will produce the image for this icon:
<svg height="128" width="128"> <ellipse cx="43" cy="32" rx="32" ry="32" style="fill:red" /> <ellipse cx="84" cy="32" rx="32" ry="32" style="fill:green" /> </svg>
Now, copy and paste the above SVG XML into a file called 'my_cusom_icon.svg':
In my case on Ubuntu 20.04, I found that if I copy this file to '/usr/share/icons/hicolor/scalable/mimetypes/THE_MIME_TYPE.svg' this will set the icon for that MIME type as it shows in the graphical file manager. For the 'THE_MIME_TYPE' part, just replace the '/' character that separates the MIME type group (in this case 'roberts-custom-mime-types') from the individual MIME type (in this case 'custom-kdenlive-project-file-mime-type') with a '-':
sudo cp my_cusom_icon.svg /usr/share/icons/hicolor/scalable/mimetypes/roberts-custom-mime-types-custom-kdenlive-project-file-mime-type.svg sudo update-icon-caches /usr/share/icons/*
For me, the change was immediately visible and I didn't have to re-log or restart:
I did, however, notice that after running 'update-icon-caches' I don't always immediately see the changes in real-time if I have a window open with files of that type. I need to re-open that folder in file manager, or even just clicking on something in that folder seems to be enough to trigger the repainting and redrawing of the updated icons.
I received an email from someone who made a note of several confusing points/clarifications that they had. Here are the notes that they provided:
- 1. The XML file doesn't like comments.
- 2. The file name matters. It must contain a dash to indicate a vendor prefix or --novendor must be used.
- 3. The file location matters. I put the xml file originally in the text subdirectory, but when I ran the xdg-mime install, the file was moved to the packages directory. When I ran the command from the packages directory the XML file was copied to the text directory without the prefix. I had to re-read step 2 & step 3 several times to understand what was going on.
- 4. xdg-mime uninstall deleted the file.
- 5. I used xdg-mime install, where you used update-mime-database. After the install I was able to get good results from mimetype -DB <file> and xdg-mime query filetype <file>, so I'm not sure of the difference between 'xdg-mime install' and 'update-mime-database'.
A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Why Is It so Hard to Detect Keyup Event on Linux?
The Most Confusing Grep Mistakes I've Ever Made
Use The 'tail' Command To Monitor Everything
Using A Piece Of Paper As A Display Terminal - ed Vs. vim
An Introduction To Data Science On The Linux Command Line
Why Bother Subscribing?