5

I am trying to find the mime-type of a file. I used either 'file --mime-type' or 'xdg-mime', but get different answers? I am not sure to understand?

$ file --mime-type -b gePlugin.kmz 
  application/zip
$ xdg-mime query filetype gePlugin.kmz 
  application/vnd.google-earth.kmz

Why do I have different answers? Which one should I rely on?

Why is there a difference?

Thanks!

xiota
  • 4,849
Matifou
  • 708
  • 1
  • 7
  • 24
  • 1
    Related to https://askubuntu.com/questions/7517/what-is-the-relationship-between-mime-types-and-file-extensions?rq=1 – user.dz Nov 02 '16 at 21:49
  • FWIW in your kmz example neither is wrong, the xdg-mime is just giving more information about a program that can use that data. It's zipped application data, as file says; but the specific application data can be used by Google Earth as xdg-mime says. They're directed towards different purposes. – pbhj Jun 30 '18 at 19:54

4 Answers4

6

@jdpipe appears to be correct regarding the behavior of xdg-mime and file. There is another utility mimetype that behaves similarly to xdg-mime.

As far as which utility to "trust", it's usually fine to just use mimetype or xdg-mime. However, if you are concerned that a file may have the wrong extension, use file because it examines the contents of the file before reporting its type. For example, if a png is renamed as jpg, file correctly reports "image/png", but xdg-mime and mimetype both incorrectly report "image/jpeg".

As noted by @db429, it appears both utilities are correct for the kmz file you cite. Google Earth creates kmz files, which are also renamed zip files. What you want to do with the information determines which application to use. If you want to extract the contents, then file tells you to go ahead and use unzip. If you want to view and use the information as originally intended, use Google Earth or another program capable of using kmz files.

Regarding the iso file, I do not know why xdg-mime is telling you model/x.stl-binary. On my computer, it reports application/x-cd-image. Also, using file without the --mime-type flag reports additional information that confirms it is some type of disk image. Perhaps it would be helpful to update the mime database:

sudo update-mime-database /usr/share/mime
xiota
  • 4,849
  • There is no one-to-one relation between extensions and MIME types (AKA Internet Media Type). For eg .iso is also used for ISO 2709, a file format very common for library bibliographic records. – Pablo Bianchi Jul 01 '18 at 01:58
  • Most probably the mime database got corrupted at some point in my case. –  Jul 02 '18 at 12:57
  • @cipricus Did update-mime-database fix it? – xiota Jul 02 '18 at 13:05
  • @xiota - for extraneous reasons I have reinstalled the system where that happened, but I hope I'll be able to remember your advice next time.:) –  Jul 02 '18 at 13:15
  • @cipricus Hope there is no next time – xiota Jul 02 '18 at 13:20
  • xdg-mime is reporting iso files as model/x.stl-binary due to a bug in the shared-mime-info package. This bug has been fixed upstream, but the fix has not yet been applied to Kubuntu 18.04. I was able to fix my own installation by removing the magic stanza indicated in the bugfix patch from the file /usr/share/mime/packages/freedesktop.org.xml, then regenerating the mime cache using the command shown in this answer. – ven42 Dec 25 '19 at 08:51
  • There is no definitive answer. If extensions are wrong, xdg-mimes output may be useless. If magic numbers are wrong, files output may be useless. Suggesting that file is more trustworthy, is not helpful. I have e.g. a lot of plain-text files with different extensions, and file is absolute garbage at diagnosing which type and program is associated with a certain file. – tifrel Apr 20 '20 at 09:49
5

The xdg-mime utility from xdg-utils is a more comprehensive program that takes into account your desktop environment (GNOME and relatives, or KDE). In some cases it makes a call to the file utility, but in other cases it looks up the mimetype database for your particular system.

The file utility, as far as I can tell, only looks inside your file, it doesn't look at the filename. Hence the .kmz file, is recognised as a zip file. file makes use of 'magic numbers' which are associated with particular file types, eg the magic string P5 at the start of a file is associated with PGM images from the Netpbm library.

The mimetype database knows about higher-level filetypes, as identified using filename extensions, and can be user-customised. When new programs are installed, the mimetype database is extended and will give answers that might not have been given before certain software was installed. To my knowledge, that behaviour doesn't happen with the file command.

If you want to read more, the xdg-mime details are here -- source code. You can see that it calls file in some cases.

jdpipe
  • 779
2

As far as I can tel in 18.04 (more here):

xdg-mime query filetype YOURFILE

is the one that is reliable.

If you need the mime type to have a context menu action with some file manager like Nautilus or Dolphin, look in that file manager under the file's "Properties".

In Dolphin:

enter image description here

0

A short web-search with google earth file format gave the answer xdg-mime has the more precise answer, yet the 'z' most likely stands for a 'zipped' fileformat, so first is true too.

d1bro
  • 2,304