== How _xdg-mime_ searches for MIME type handlers (more or less) XDG is what was once called the X Desktop Group and is now [[freedesktop.org https://www.freedesktop.org/]]. Part of their work is to create various cross-desktop and desktop agnostic standards and tools for how to do things like determine what program should handle a particular MIME type. This includes special 'scheme' MIME types, like _x-scheme-handler/ftp_, which means 'whatever should handle FTP' (technically this is 'FTP urls'). The XDG tool for mapping MIME types to programs (actually _.desktop_ files) is [[_xdg-mime_ https://portland.freedesktop.org/doc/xdg-mime.html]]. Like all of the XDG tools, _xdg-mime_ uses a collection of environment variables, which will normally have the default values covered in [[the XDG base directory specification https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html]]. In theory there are two sorts of data that _xdg-mime_ looks at. The first, found in files called _-mimeapps.list_ and _mimeapps.list_, is supposed to be a set of specifically chosen applications (whether configured by the user or the system). The second is a general cache of MIME type mapping information based on what program can handle what MIME types (as listed in each program's _.desktop_ file); these are found in files called _defaults.list_ and _mimeinfo.cache_. In practice, a system _mimeapps.list_ file may have just been slapped together based on what the distribution thought was a good idea, and it may not correspond to what you want (or even what you have installed). (Not all Linux distributions ship a system _mimeapps.list_. Fedora does; Ubuntu doesn't.) Xdg-mime searches for _mimeapps.list_ in any of (($XDG_CONFIG_HOME)), (($XDG_CONFIG_DIRS)), and the '_applications_' subdirectories of (($XDG_DATA_HOME)) and (($XDG_DATA_DIRS)). It searches for the other two files only in the _applications_ subdirectories of (($XDG_DATA_HOME)) and (($XDG_DATA_DIRS)). Now, we get to an important consequence of this. (($XDG_DATA_DIRS)) is normally two directories, _/usr/local/share_ and _/usr/share_, each of which has MIME type information only for itself, and they are checked in order. The normal result of this is that ~~things installed into _/usr/local_ steal MIME types from things installed in _/usr_~~ because _xdg-mime_ will check the _/usr/local/share/applications_ files first. (I discovered this when I installed Google Chrome on my laptop and it promptly stole URL handling from Firefox, which I did not want, because the RPM put bits into _/usr/local_ instead of _/usr_. Actually finding the files that controlled this was remarkably hard.) Normally, nothing automatically generates or updates the system _mimeapps.list_; on Fedora, it's part of the _shared-mime-info_ RPM (and _kde-settings_ for the _kde-mimeapps.list_ version). The _mimeinfo.cache_ files are maintained by [[_update-desktop-database_ http://manpages.ubuntu.com/manpages/precise/man1/update-desktop-database.1.html]], which will normally be automatically run on package installs, removals, and probably upgrades. Now, _xdg-mime_ does not give you an actual program to run. What it does is give you the name of a _.desktop_ file, eg _firefox.desktop_. In order to use this to run a program, you must look through the right magic places to find the _.desktop_ file and then parse it to determine the command line to run. Probably you don't want to do this yourself, but I don't know what your alternative is; as far as I know, there is no XDG tool script to say 'run this _.desktop_ command with the following arguments'. (Note that the _.desktop_ file is not necessarily in the same directory as the MIME mapping file that caused it to be the handler application. For example, your _$HOME/.local/share/applications_ might just have various MIME type overrides you've set for what system application should handle what.) This explanation is somewhat dependent on what exact version of the XDG tools and scripts you have installed. It's also not necessarily totally complete, because I am reading through undocumented shell scripts here and I've left a few things out. See [[the Arch wiki entry on default applications https://wiki.archlinux.org/index.php/default_applications]] for much more information. PS: If you're trying to track down why _xdg-mime_ is giving you some strange result, set (($XDG_UTILS_DEBUG_LEVEL)) to at least '2'. This will tell you just what files it looked at when, although I don't think it ever reports what directories it looked at but didn't find any files in.