Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My xdg-mime makes mistakes. It recognizes an .html file containing Java code as a C++ file.

I don't quite know the internal mechanism of xdg (eg. how it determines filetype). And I want to know how to fix this problem by giving preference to judgement by filename extensions.

Of course other solutions are welcome. And explanation of mechanisms of xdg-* is even more welcome.

share|improve this question
@ don_crissti Hello, I don't use DE. I use a bare WM Openbox. I know xdg-mime will retransmit the file type analysis to DE specific modules. But in case of WM, I don't know whether xdg-mime has internal analyzer. – cyker Sep 9 '12 at 21:09
up vote 1 down vote accepted

xdg-mime is part of a collection of scripts called xdg-utils, "a set of common interfaces for desktop environments (DE)". In your particular case, I'll just quote MestreLion's comment from here:

If you dont have any desktop enviroment, you should not use xdg-mime *(or any other xdg tool). xdg is meant to provide interoperability* between different desktop enviroments, but not when there is none. *Think of xdg as desktop-agnostic, but not "desktop-atheist"*

As to the internal mechanics of xdg-utils... They're just shell scripts checking for a DE and, IF found, calling that particular DE's file tools to perform a certain task. In GNOME xdg-open calls gvfs-open and xdg-mime calls gvfs-info (both gvfs-* tools giving preference to judgement by file extension). In your case, no DE is found so xdg-mime falls back to file[1] to get information about the file content type. Here is the code section from xdg-mime for "generic" DE (or rather unknown):

info_generic() {
DEBUG 1 "Running file -i \"$1\""
/usr/bin/file -i "$1" 2> /dev/null | cut -d ":" -f 2 | sed s/"^ "//

if [ $? -eq 0 ]; then
fi }

Though it is true file[1] results are not always 100% accurate, there's not much you can do about that (it's still the best tool for this job IMO). However, if you want to determine the mime types based only on files extensions (a la windows) you could write your own script. Here is a simple python example:


import os
import sys
import mimetypes

if len(sys.argv) != 2:
    print ("Usage: ./pymime filename")
    raise SystemExit

testfile = os.path.abspath(sys.argv[1])
mt = mimetypes.guess_type(testfile, strict=True)[0]
print ("%s" % mt)

Name it pymime and run:

./pymime yourfile
share|improve this answer
Great answer! Thank you. Finally I decide to use mimeo with it's own version of xdg-mime which is more accurate. – cyker Sep 10 '12 at 5:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.