Shell – “xdg-mime query filetype” returning different filetype than “file –mime-type”

mime-typesshell

I have a bash script that runs to create emails. I do not wish to modify it for now since it is pretty critical in my current project. One command does not run the same on the operational machine and on mine:

xdg-mime query filetype <file>

It's running on a simple us-ascii encoded text file (with a custome file extension). The thing is, on the operational machine where the script works, it returns plain/text (expected behavior). Debug mode of xdg-mime shows that it is actually running a file -i command on the operational machine. On my machine though, it returns application/octet-stream and runs a gnomevfs-info command. It appears to have something to do with the desktop environment (both machines are running on gnome).

Is there a way to force xdg-mime to run file -i? Or to make gnomevfs-info return the correct mime type ? I tried unsetting GNOME_DESKTOP_SESSION_ID but here is what xdg-mime does:

detectDE()
{
    if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
    elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
    elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
    elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
    fi
}

The third elif is the one that leads to xdg-mime using gnomevfs-info over file -i, because the command returns 0 and DE is set to gnome. I've tried looking at man pages, but that command dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1 is jibberish to me.

Best Answer

If you know your custom text file extension(s) in advance, you can simply register a new MIME entry for them, as a workaround.

E.g. if your files have an extension of ".list":

Prepare the descriptor file: zeppelin-list.xml

<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
  <mime-type type="text/plain">
    <comment>List file type</comment>
    <glob pattern="*.list"/>
  </mime-type>
</mime-info>

Register a new MIME entry:

%xdg-mime install zeppelin-list.xml

Query:

%xdg-mime query filetype my.list
text/plain
Related Question