Ubuntu – How to determine the MIME type of a file

bashmime-type

I recently switched to GNOME 3 and was surprised to find that there was no way by which we could set the default applications. I am writing a Nautilus extension using Bash. I want to find the MIME type of a file.

Initially I used to extract the extension of the file using sed and build the code on it. Then I realized that there is this command called file. When I try to find the mime-type of a mkv file, the command file --mime-type -b <filename> outputs application/octet-stream but when I see the Nautilus properties window it shows the correct video/x-matroska mime-type.

Am I missing anything here? If not is there a better way in which I can file the mime-type of a file?

Best Answer

There are different ways to get a MIME type on Linux, and they often lead to different results. Use

xdg-mime query filetype your_file

to get the same MIME type as Nautilus gets.