How to Determine MIME Type of a File in Linux

If you are a Linux user whose intuitions are strongly allied with web technology then the concept of MIME types should be imprinted in your DNA.

MIME types help identify file formats and formatted contents during their transmission across the internet or any other user-defined network.

While on a web browser and you receive a webserver-sent file via HTTP before the web browser chooses a suitable method for displaying the file, the web browser will first consult the MIME types to determine the file type it is about to handle.

Conceptualizing MIME Types

MIME or Multi-Purpose Internet Mail Extensions Types, helps internet users standardize file types classification. To understand MIME Types, let us look at a common MIME Type example like the following:

text/html

In reference to the above MIME Type example, we can note that it has two parts (type and subtype) which is a key MIME Type attribute. In this case, the text is the type and html the subtype.

Other common MIME Types examples include the following:

application/pdf
application/zip
application/xml
text/xml
text/csv
text/plain
multipart/form-data    

Note that both the type and subtype elements of a MIME Type are case sensitive. You are right to assume that the subtype portion of a MIME Type are mostly media formats like pdf, xml, zip, and csv. However, the MIME Type subtype can also contain tree suffix or prefix as per the following standard MIME Type format:

type "/" [tree "."] subtype ["+" suffix] 

A perfect MIME Type example in reference to the above complete MIME Type format is as follows:

application/vnd.api+json

The above example is the JSON API format of an API-specific MIME Type where the type is an application, the subtype is API, the vendor prefix is vnd., and the suffix is +json.

Determining a File’s MIME Type in Linux

While MIME Types have standardized the naming of file types and their associated formats, this functionality is neither stored nor automated in the Linux filesystem.

To correctly determine a targeted file’s MIME Type under Linux, we have to acknowledge the file’s extension and content.

  • MIME Types by File Extension – If the file extension is missing or incorrect like the case where a zip file is renamed to have a JPG image file extension, then determining its MIME Type by file extension will be meaningless.
  • MIME Types by File Content – The file content’s specific characteristics like the FF D9 and FF D8 hex signatures in JPG image files help determine the associated MIME Type. However, the extra I/O efforts used in this approach make it slower but still reliable.

Find File MIME Types Using xdg-mime Command

Since the xdg-utils package hosts the xdg-mime command, it should be available in almost all Linux operating system distributions. It determines the MIME Type only by file extension.

Its reference syntax is as follows:

$ xdg-mime query filetype TARGETED_INPUT_FILE 

Let us try to determine the MIME Types of the following files:

$ xdg-mime query filetype mypic.jpeg
$ xdg-mime query filetype sample_file.txt
Find File MIME Types in Linux
Find File MIME Types in Linux

We can also remove the image file’s extension and still get its MIME Type through this command:

$ mv mypic.jpeg mypic
$ xdg-mime query filetype mypic 
Find MIME Types of File in Linux
Find MIME Types of Files in Linux

Print File MIME Types Using file Command

The file command is a default installation in almost all Linux operating system distributions. It determines a file’s MIME Type both by file extension and by file content.

Its reference/usage syntax is as follows:

$ file --mime-type TARGETED_INPUT_FILE

We can implement it in the two showcased file types as follows:

$ file --mime-type mypic.jpeg
$ file --mime-type sample_file.txt
Print File MIME Types in Linux
Print File MIME Types in Linux

Let us also remove the JPEG image file extension and monitor the output of this command:

$ mv mypic.jpeg mypic
$ file --mime-type mypic
Find File MIME Types by Extension
Find File MIME Types by Extension

xdg-mime vs file Commands Performance Accuracy

If we change the file extension of a random text file to a zip file format and check the MIME Type of this file using both xdg-mime and file commands, the file command will be able to correctly determine the MIME Type based on the file content.

$ mv output.txt output.zip
$ file --mime-type output.zip
$ xdg-mime query filetype output.zip 
Correctly Find File MIME Type in Linux
Correctly Find File MIME Type in Linux

We can now determine the MIME Type of a file based on file extension, file content, or both in Linux.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.