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!
@jdpipe appears to be correct regarding the behavior of
xdg-mime
andfile
. There is another utilitymimetype
that behaves similarly toxdg-mime
.As far as which utility to "trust", it's usually fine to just use
mimetype
orxdg-mime
. However, if you are concerned that a file may have the wrong extension, usefile
because it examines the contents of the file before reporting its type. For example, if apng
is renamed asjpg
,file
correctly reports "image/png", butxdg-mime
andmimetype
both incorrectly report "image/jpeg".As noted by @db429, it appears both utilities are correct for the
kmz
file you cite. Google Earth createskmz
files, which are also renamedzip
files. What you want to do with the information determines which application to use. If you want to extract the contents, thenfile
tells you to go ahead and useunzip
. If you want to view and use the information as originally intended, use Google Earth or another program capable of usingkmz
files.Regarding the
iso
file, I do not know whyxdg-mime
is telling youmodel/x.stl-binary
. On my computer, it reportsapplication/x-cd-image
. Also, usingfile
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:The
xdg-mime
utility fromxdg-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 thefile
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 stringP5
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 callsfile
in some cases.As far as I can tel in 18.04 (more here):
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:
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.