How to assign (set) a MIME type to a file?
Question is already answered by @PHPLearner in a comment. However, here is a longer answer.
There is no particular command like mime as asked in the question, and no doubt one such command can be created. For adding a new MIME type, all it takes is editing the /etc/mime.types file.
Let's say you want to add MIME type with extension .btc, then
1. Check If MIME type already exists
Open a command line and enter the line below (replace btc with your extension)
grep 'btc' /etc/mime.types
Now, this command will output a line, If that MIME type is already added. It looks like this for particular MIME searches
$ grep 'cpp' /etc/mime.types
text/x-c++src c++ cpp cxx cc
$ grep 'py' /etc/mime.types
application/x-python-code pyc pyo
text/vnd.debian.copyright
text/x-python py
$ grep 'btc' /etc/mime.types
If your extension does not output any lines (as for btc in this case), or if the lines outputed do not include your extension, you must create a new MIME type. Otherwise your extension already has a MIME type included in the file /etc/mime.types.
2.1 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. For that type at command line
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. Here our extension is bitcoin and we write btc (NOT .btc) that will be seen as an extension for the bitcoin files.
text/extension extension
And copy the modified 'text/extension' part.
In our case it will look like
text/bitcoin-text btc
Save the file and exit.
2.2 Adding MIME type using .xml file and update-mime-database
If editing /etc/mime.types file doesn't works for your extension, then you can try this workaround.
Create a new .xml file that describes your extension like this & Save it.
<?xml version="1.0" encoding="utf-8"?>
<mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-info type="text/bitcoin-text">
<glob pattern="*.btc"/>
</mime-info>
</mime-type>
Now add this file into /usr/share/mime/packages folder (ref).After you've added or modified whatever you need, run the command
sudo update-mime-database /usr/share/mime
3. Adding an Icon to MIME type
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.
So, We rename the .svg file so that the it matches bitcoin-text.svg (or "insertYourMIMEtype.svg") so that the slashes are replaced with "-" and there are no capital letters.
Then simply run the following commands, with 'bitcoin-text' replaced with your MIME type.
sudo cp bitcoin-text.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.
Related videos on Youtube
PHP Learner
I decided to create a web site of my self, using PHP language. While learning PHP, I became familiar with Linux and Ubuntu and find it better than Microsoft Windows. Now trying to learn both PHP and Linux (Ubuntu) simultaneously.
Updated on September 18, 2022Comments
-
PHP Learner 3 monthsIs there any command that could set MIME type of a file? for example:
mime --set --MIME="image/pjpeg" filename.jpg-
Rmano almost 8 yearsIt's quite old, but maybe it is still useful: rlog.rgtti.com/2010/11/22/… (sorry, no time to dig it out now...). Please add an answer yourself if it works! -
Sylvain Pineau almost 8 yearspossible duplicate of How do I change the MIME type for a file? -
PHP Learner almost 8 years@SylvainPineau The question you linked is similar but not a duplicate and There is no answer to my question in that link, nor any answer to the link question itself! Based on accepted answer in that link, problem of asker was not MIME type, but it was the file content itself. -
Sylvain Pineau almost 8 years@PHPLearner Look at help.ubuntu.com/community/AddingMimeTypes, especially the use of xml files andupdate-mime-database -
Sylvain Pineau almost 8 years@PHPLearner: I've removed my close vote -
PHP Learner almost 8 years@SylvainPineau It seems that the link you sent is not complete explanation. Not all MIME types are stored in/etc/mime.types, becausegrep 'otf' /etc/mime.typesandgrep 'ttf' /etc/mime.typesdoes not return any result on my ubuntu 14.04, but these MIME types are defined in my system and are shown in Nautilus file properties as: OpenType font (application/x-font-otf) and TrueType font (application/x-font-ttf) respectively!
-
-
Vlax about 3 yearsyou actually need to add the xml file to /usr/share/mime/packages, if you add it to /usr/share/mime/application when you run update-mime-database it will be wiped out -
C0deDaedalus over 1 yearThanks @Vlax for pointing it out. -
C0deDaedalus over 1 yearThansk @vanadium for editing.