How does Ubuntu or any other Linux systems store file associations?
Is there some /etc/asscociations
file or something?
I know that I can right click on file and change it via "open with", but I'm just curious to know how it's stored internally.
How does Ubuntu or any other Linux systems store file associations?
Is there some /etc/asscociations
file or something?
I know that I can right click on file and change it via "open with", but I'm just curious to know how it's stored internally.
The file manager (Nautilus, by default) uses the MIME type of a file to determine which program to open it with. When an application is installed, it can specify what MIME types it can open and the command to use to open the files in the .desktop file which is placed in
/usr/share/applications
. This is the file used for menus, desktop shortcuts, etc.For example, GIMP has the following .desktop file:
See the
MimeType
field - this lists the supported MIME types.TheExec
field tells the system to use the commandgimp-2.7 %U
, replacing '%U' with the files to open. (Note GIMP 2.7 is a version I have installed from a PPA, so is higher than the current version in the Ubuntu repositories).When the application package is installed, the system extracts this MIME type data into a more easily accessible database because looking in each .desktop file would take too long if it was done every time a file was opened.
This tells the system what applications can be used for that MIME type and provides the applications in the 'Open With' list. The default is defined elsewhere. The file
/usr/share/applications/defaults.list
provides the information for the system defaults. Unless you choose otherwise, these are the applications used when you 'Open' a file.To supplement dv3500ea's excellent answer, I would like to add some information about what happens when you change your associations.
While the defaults.list (which you can find by typing 'locate defaults.list') provides the list of applications that are associated with each MIME type, any customizations that you make are stored in your home directory, in ~/.local/share/applications/mimeapps.list. So if you add or remove associations, or change the default association, this file is updated.
The GNOME Desktop System Administration Guide at http://library.gnome.org/admin/system-admin-guide/stable/ does not appear to discuss mimeapps.list, but I found the following description on http://live.gnome.org/SysAdminGuideUpdate:
I found that on my system (Debian Jessie) there's also a
~/.config/mimeapps.list
that had an association I accidentally created to open a specific file type with gedit. None of the standard ways of correcting this (default application settings, Thunar's open with property) reflected this, but gedit was the default application.I was able to remove the line from
~/.config/mimeapps.list
and now the correct application opens the file.I was able to set an association like this:
You can also remove associations and do other things:
man xdg-mime
I did not need to run
sudo update-desktop-database
.I found this link concerning default association, it might be helpful. https://wiki.archlinux.org/index.php/Default_Applications
for global association:
per user association:
syntax is as follow:
[Added Associations] section is used to specify preferred (default) applications in decreasing preference. which means desktopfile1 is the most preferred and desktopfileN is least preferred.
[Removed Associations] section is used to explicitly remove any previously inherited associations.
Create a file
truecrypt.desktop
in~/.local/share/applications
with the following lines:then edit the file
~/.local/share/applications/mimeapps.list
and add the following line:I found out the mime type of Truecrypt by following running this command:
mimeapps.list
search pathOthers have mentioned this file, but none have explained it's location precisely.
The freedesktop.org standard says at https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.html :
and then the default values for those environment variables is given at https://specifications.freedesktop.org/basedir-spec/basedir-spec-0.6.html :
$XDG_DATA_HOME
:$HOME/.local/share
$XDG_CONFIG_HOME
:$HOME/.config
$XDG_DATA_DIRS
: none, appended toXDG_DATA_HOME
$XDG_CONFIG_DIRS
: none, appended toXDG_CONFIG_HOME
Older versions of Ubuntu may not check
~/.config
, so make sure you try~/.local/share
as well.Commands like:
will automatically edit those files.
On Ubuntu 18.04, I've found the order of precedence to be as follows, from strongest to weakest:
~/.local/share/applications/defaults.list
/usr/share/applications/defaults.list
~/.config/mimeapps.list
("Open With Other Application" will update this)~/.local/share/applications/mimeapps.list
mimeinfo.cache
registry of compatible applications, which are generated from the.desktop
files present in/usr/share/applications
and/usr/local/share/applications
, and can be regenerated usingupdate-desktop-database
./etc/gnome/defaults.list
Freedesktop.org (previously called the X Desktop Group, thus the "XDG" abbreviation) is the group currently driving X standards like this (learn more at Wikipedia).
warvariuc's answer already mentions xdg-mime usage like:
After lots of playing with image editors and viewers, I ended up with different viewers for different image types with almost no consistency, especially among the rarer image types.
I took the above command and assigned it to each of the MIME types controlled by the image viewers and then created a new
image-viewer.desktop
by cloning one of them (this is optional, you can merely reuse your favorite). Next, I manually verified that none of the MIME types would be incompatible with my new preferred application. Finally, I set that application to be the default for all of the other applications' MIME types:This examines the
.desktop
files for each of the viewers (Ristretto, Geeqie, and GThumb in my case), extracts the MIME definition (which is prefaced byMimeType=
and then contains a semicolon-delimited list), removes the (previously matched) preface label, and splits the list into one entry per line.sort -u
then removes the redundant lines, then they're handed toxdg-mime
to set the new defaults.(If you want to test this before running it live, change
xargs
toxargs echo
and it will print the resulting command instead of running it.)This technique should work for anything you want to reclaim for a target app. You don't even need to create a new
.desktop
file (you can use an existing one). I did that extra step so that when I inevitably change my allegiance between image previewers, I merely need to change that one file.For all users:
Each user has a config file for overriding these defaults:
see also https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.1.html for places that might be used for overriding defauts.
You must know the mime-type of the file you want to change it's association.
Also all associations can be handled-changed in command line using: