I found that the mimetype program from Perls File-MimeInfo module usally gives better results for mimetypes. So I suggest using it as a default and only use the standard file program as a fallback option. mimetype should also already be installed on many distributions.
An alternative would be to use the xdg-mime (from xdg-utils) program, which uses mimetype as a default and file as a fallback. But it has a slightly more complex syntax and I don't really know how it behaves with desktop enviroments. (But I think it is unaffected by the current enviroment and only xdg-open takes the current DE into account.)
I found that the
mimetype
program from Perls File-MimeInfo module usally gives better results for mimetypes. So I suggest using it as a default and only use the standardfile
program as a fallback option.mimetype
should also already be installed on many distributions.The reason
file
gives less accurate output, is that it does not follow the XDG standard and so some registered mimetypes are not detected by it. (Sources: https://wiki.archlinux.org/title/Default_applications#perl-file-mimeinfo and https://wiki.archlinux.org/title/Xdg-utils#xdg-open) On my system for example, it fails to correctly identify Xournall++ files.file
detects them as anapplication/gzip
, whilemimetype
correctly detects them as anapplication/x-xopp
.An alternative would be to use the
xdg-mime
(fromxdg-utils
) program, which usesmimetype
as a default andfile
as a fallback. But it has a slightly more complex syntax and I don't really know how it behaves with desktop enviroments. (But I think it is unaffected by the current enviroment and onlyxdg-open
takes the current DE into account.)