Introduction
MIME types are a sort of identification card for files on Ubuntu. A file's MIME type is usually determined by the extension of its filename; for example, a text file may end in .txt, and image file might end in .png or .jpg. This tells Ubuntu what icon should be used to represent it and what programs should open it by default.
Unfortunately, the obvious options for editing MIME types in Ubuntu are shallow. You can teach Ubuntu to open a certain file extension with a certain program using the Properties window, but you cannot set an icon for that extension as a whole: only for the individual file. This can be annoying if you are say, a Python programmer. Ubuntu does not include a special icon for Python files, and it is hard to visually differentiate .py and .pyc files.
This guide will both teach you to add new MIME types and to associate icons with existing types. We will use .py files as an example.
Adding a MIME Type
To start out, we must be assure that the MIME type for our file extension does not already exist. Open a command line and enter the line below, replacing the letters py with your extension.
grep 'py' /etc/mime.types
Analysing results
This particular command will output two lines. If your extension does not output any lines, or if the lines outputed do not include your extension, you must create a new MIME type. Otherwise, look at the right side of this output, find your file extension, and copy the corresponding text to the far right. For 'py', we must copy down 'text/x-python'.
application/x-python-code pyc pyo text/x-python py
Creating the MIME type, if needed
If there was no output, or the output given did not include your extension, we must add a MIME type.
gksudo gedit /etc/mime.types
Modify the following text so that the word "extension" is replaced with your file extension (no period mark), add the line to the end of the mime.types file, and save.
text/extension extension
And copy the modified 'text/extension' part.
Another way to add a MIME Type
On 12.04LTS, I edited the /etc/mime.types file and it had no effect. What worked for me was to add xml files in /usr/share/mime/application; for example here's a file (scad.xml) to describe scad files:
<?xml version="1.0" encoding="utf-8"?> <mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info" type="application/scad"> <glob pattern="*.scad"/> </mime-type>
After you've added or modified whatever you need, run the command
sudo update-mime-database /usr/share/mime
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.
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.