Ubuntu – Why the difference between the results with “file –mime-type” and “xdg-mime query filetype”

mime-type

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!


UPDATE (by cipricus):

Example for iso file:

file --mime-type -b MY_ISO.iso gives application/x-iso9660-image, which is not useful with a desktop file for context menu actions.

xdg-mime query filetype MY_ISO.iso gives model/x.stl-binary, which works with te same desktop for context menu actions.

Best Answer

@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