Is there any command that could set MIME type of a file? for example:
mime --set --MIME="image/pjpeg" filename.jpg
Is there any command that could set MIME type of a file? for example:
mime --set --MIME="image/pjpeg" filename.jpg
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
, then1. Check If MIME type already exists
Open a command line and enter the line below (replace
btc
with your extension)Now, this command will output a line, If that MIME type is already added. It looks like this for particular MIME searches
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
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 writebtc
(NOT .btc) that will be seen as an extension for the bitcoin files.And copy the modified 'text/extension' part.
In our case it will look like
Save the file and exit.
2.2 Adding MIME type using
.xml
file andupdate-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.Now add this file into
/usr/share/mime/packages
folder (ref).After you've added or modified whatever you need, run the command3. 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 areno capital letters
.Then simply run the following commands, with 'bitcoin-text' replaced with your MIME type.
Relogin and all files ending in the MIME extension will display with that icon.
To actually answer your question: MIME types aren't real. There isn't some piece of metadata in the file that says "this is a image/png". Instead, MIME types are guessed based on file extension and magic number. To serve a file as having a specific MIME type, you need to configure your web-server appropriately. See https://stackoverflow.com/questions/29017725/how-do-you-change-the-mime-type-of-a-file-from-the-terminal