81

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.

Flyk
  • 1,480
Stann
  • 15,116

11 Answers11

71

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:

[Desktop Entry]
Version=1.0
Type=Application
Name=GNU Image Manipulation Program
GenericName=Image Editor
Comment=Create images and edit photographs
Exec=gimp-2.7 %U
TryExec=gimp-2.7
Icon=gimp
Terminal=false
Categories=Graphics;2DGraphics;RasterGraphics;GTK;
X-GNOME-Bugzilla-Bugzilla=GNOME
X-GNOME-Bugzilla-Product=GIMP
X-GNOME-Bugzilla-Component=General
X-GNOME-Bugzilla-Version=2.7.2
X-GNOME-Bugzilla-OtherBinaries=gimp-2.7
StartupNotify=true
MimeType=application/postscript;application/pdf;image/bmp;image/g3fax;image/gif;image/x-fits;image/pcx;image/x-portable-anymap;image/x-portable-bitmap;image/x-portable-graymap;image/x-portable-pixmap;image/x-psd;image/x-sgi;image/x-tga;image/x-xbitmap;image/x-xwindowdump;image/x-xcf;image/x-compressed-xcf;image/tiff;image/jpeg;image/x-psp;image/png;image/x-icon;image/x-xpixmap;image/svg+xml;application/pdf;image/x-wmf;image/jp2;image/jpeg2000;image/jpx;image/x-xcursor;

See the MimeType field - this lists the supported MIME types.The Exec field tells the system to use the command gimp-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.

dv3500ea
  • 37,204
  • 8
    After changing MIME types in /usr/share/applications .desktop files, is there a command to re-create that MIME database to see new associations in action? – Redsandro Oct 10 '12 at 16:40
  • 12
    Yes. sudo update-desktop-database See http://jarrpa.net/2011/10/28/creating-custom-url-handlers-in-ubuntu-11-04-11-10-gnome-3-0/ – NoBugs Dec 22 '12 at 04:13
  • A more appropriate place for local changes is /usr/local/share/applications. I think gtk looks there, too, by default. It does on my system, but it's been upgraded (not reinstalled) since Ubuntu 7.04 or so. – Peter Cordes Feb 03 '15 at 22:37
  • 2
    Ubuntu seems to follow freedesktop.org: http://standards.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.1.html , an awesome project. – Ciro Santilli OurBigBook.com Mar 30 '15 at 19:44
  • 1
    FWIW, I had to use sudo update-desktop-database ~/.local/share/applications/ because the .desktop files were local. – Michael Jan 15 '20 at 03:30
  • Maybe add, that user specific information is stored in ~/.local/share/applications/defaults.list – DarkTrick Mar 30 '20 at 13:44
  • Every time I run qtfm it overwrites ~/.local/share/applications/mimeapps.list. I have no defaults.list files anywhere. I've removed any mimeinfo.cache files I could find and they haven't been re-created. I've confirmed the /usr/share/applications/mupdf.desktop entry has a application/pdf mime type. I've regenerated the database. I have no /usr/local/share/applications directory. I can't even see where qtfm is getting it's defaults from but when it's supposed to support mimeapps.list I don't see how "support" involves overwriting what should be the user preferences. – NeilG Jul 23 '23 at 02:11
27

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:

mimeapps.list is located in $XDG_DATA_DIRS/applications. Its purpose is to add or remove mime associations from applications. nautilus writes ~/.local/share/applications/mimeapps.list when the user makes changes in the "Open With" dialog.

  • 1
    But using Ubuntu 12.04, I realize that the mimeapps.list is overridden by entries in the defaults.list. – feeela Jun 25 '12 at 12:55
  • 1
    on 14.04, I was able to s/vlc/mplayer/ in my ~/.local/share/applications/mimeapps.list, and running processes like pcmanfm picked up the changes without restarting. (mplayer.desktop is just runs mplayer. I had one lying around from an old mplayer install in /usr/local, I guess. IDK if having a MimeType=list; was needed or not. – Peter Cordes Feb 03 '15 at 22:35
  • 1
    More precisely, mimeapps.list can be anywhere in the following search path: https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.1.html – Ciro Santilli OurBigBook.com Jan 19 '17 at 09:10
  • I find qtfm mercilessly overwrites ~/.local/sahre/applications/mimeapps.list from some unknown set of defaults regardless of what I do :-(. The QtFM release notes call this "support". – NeilG Jul 23 '23 at 02:13
26

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.

Jens Erat
  • 5,051
  • 7
  • 31
  • 37
  • 3
    Thanks, you accidentally helped me to fix Firefox opening everything in Gedit. On openSUSE 13.2 I removed the line with "application/octet-stream=org.gnome.gedit.desktop;". :) – user31389 Mar 04 '15 at 21:01
  • 4
    since ubuntu 15.04 the user-specific modifications are also stored in ~/.config/mimeapps.list. – DJCrashdummy Oct 11 '16 at 07:46
  • 4
    More precisely, mimeapps.list can be anywhere in the following search path: https://specifications.freedesktop.org/mime-apps-spec/mime-apps-spec-1.0.1.html – Ciro Santilli OurBigBook.com Jan 19 '17 at 09:11
13

I was able to set an association like this:

xdg-mime default xnview.desktop image/jpeg

You can also remove associations and do other things: man xdg-mime

I did not need to run sudo update-desktop-database.

warvariuc
  • 2,305
  • Thanks for putting this answer in. I thought it might fix my problem with qtfm overwriting ~/.local/share/applications/mimeapps.list but unfortunately xdg-mime query default application/pdf still shows mupdf.desktop whether there is an entry in mimeapps.list or not, and qtfm still overwrites my mimeapps.list and doesn't respect what xdg-mime says. I wonder where the other location is for the defaults that qtfm is reading? – NeilG Jul 23 '23 at 02:21
10

On Ubuntu 18.04, I've found the order of precedence to be as follows, from strongest to weakest:

  1. ~/.local/share/applications/defaults.list
  2. /usr/share/applications/defaults.list
  3. ~/.config/mimeapps.list ("Open With Other Application" will update this)
  4. ~/.local/share/applications/mimeapps.list
  5. The first encountered match in the 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 using update-desktop-database.
  6. (Never used) /etc/gnome/defaults.list
Jimmy He
  • 280
  • 1
    I noted when in /usr/share/applications/defaults.list there are several entries on one line, only first apprears in applications to open file with (as default if not overwritten), I don't know why others are present. – Alex Martian Dec 03 '22 at 12:43
  • 2
    It seams "strongest" means more upper level, ~/.config/mimeapps.list AFAIK overwrites /usr/share/applications/defaults.list. – Alex Martian Dec 03 '22 at 12:44
  • Thanks for the extra information. My ~/.config/mimeapps.list did have (better) information in it but qtfm wasn't respecting it and was still over-writing ~/.local/share/applications/mimeapps.list from some other mysterious location. – NeilG Jul 23 '23 at 02:36
9

I found this link concerning default association, it might be helpful. https://wiki.archlinux.org/index.php/Default_Applications
for global association:

/usr/share/applications/mimeapps.list

per user association:

~/.local/share/applications/mimeapps.list

syntax is as follow:

[Added Associations]
mimetype=desktopfile1;desktopfile2;...;desktopfileN
...
[Removed Associations]
mimetype=desktopfile1;desktopfile2;...;desktopfileN

[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.

7

Create a file truecrypt.desktop in ~/.local/share/applications with the following lines:

[Desktop Entry]
Encoding=UTF-8
Type=Application
Icon=
Exec=/usr/bin/truecrypt %U
Name=truecrypt
Comment=manage truecrypt volumes

then edit the file ~/.local/share/applications/mimeapps.list and add the following line:

application/octet-stream=truecrypt.desktop

I found out the mime type of Truecrypt by following running this command:

file --mime-type -b <any truecrypt filename>
Yi Jiang
  • 1,206
  • Nice answer, because it works for those of use who use kde, but have a couple of gtk-native programs, like firefox. – naught101 Apr 24 '12 at 05:57
6

mimeapps.list search path

Others 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 :

The lookup order for this file is as follows:

Location Usage
$XDG_CONFIG_HOME/$desktop-mimeapps.list user overrides, desktop-specific (for advanced users)
$XDG_CONFIG_HOME/mimeapps.list user overrides (recommended location for user configuration GUIs)
$XDG_CONFIG_DIRS/$desktop-mimeapps.list sysadmin and ISV overrides, desktop-specific
$XDG_CONFIG_DIRS/mimeapps.list sysadmin and ISV overrides
$XDG_DATA_HOME/applications/$desktop-mimeapps.list for completeness, deprecated, desktop-specific
$XDG_DATA_HOME/applications/mimeapps.list for compatibility, deprecated
$XDG_DATA_DIRS/applications/$desktop-mimeapps.list distribution-provided defaults, desktop-specific
$XDG_DATA_DIRS/applications/mimeapps.list distribution-provided defaults

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 to XDG_DATA_HOME
  • $XDG_CONFIG_DIRS: none, appended to XDG_CONFIG_HOME

Older versions of Ubuntu may not check ~/.config, so make sure you try ~/.local/share as well.

Commands like:

 xdg-settings set default-web-browser chromium-browser.desktop
 xdg-settings get default-web-browser

will automatically edit those files.

3

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:

xdg-mime default xnview.desktop image/jpeg

 

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:

sed '/^MimeType=/!d; s///; s/;/\n/g' \
  /usr/share/applications/{ristretto,geeqie,gthumb}.desktop |sort -u \
  |xargs xdg-mime default image-viewer.desktop

This examines the .desktop files for each of the viewers (Ristretto, Geeqie, and GThumb in my case), extracts the MIME definition (which is prefaced by MimeType= 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 to xdg-mime to set the new defaults.

(If you want to test this before running it live, change xargs to xargs 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.

Adam Katz
  • 784
  • 8
  • 18
3

For all users:

/usr/share/applications/defaults.list

Each user has a config file for overriding these defaults:

~/.config/mimeapps.list

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:

xdg-mime

0

I'm adding this as an answer rather than a comment as I've been on this all morning and others may find this a crucial aspect of the file associations on Linux.

Free Desktop Specification

As linked in the references, the Free Desktop specification describes how the mimeapps.list file(s) are used to determine which applications open which types of files.

ArchLinux wiki also has some great references, also linked below.

Other answers go into this in sufficient detail.

"Hidden" and "NoDisplay" Entries

To get to the point of this answer, you may not have noticed that your desktop entry file (<application>.desktop file usually found in /usr/share/applications) may have a line where it states that Hidden=True or NoDisplay=True.

"Hidden" Entries

"Hidden" does not mean just invisible. It means "deleted" or exactly the same as "just not there". If your desktop entry is "Hidden" not only is it as good as deleted, but aggressive file managers (such as QtFM) may strip it from your mimeapps.list file on start up.

If you're running into problems, just remove the "Hidden" line and your troubles may be over.

"NoDisplay" Entries

Well, the Free Desktop spec is very clear and explicit about "NoDisplay", but it turns out not all applications may be so clear.

"NoDisplay" means "don't list the application in menus", but do respect it and use the entry for launch, even as the default handler, even though it's not displayed in the menus. There are genuine use-cases for this, and that's why the "NoDisplay" feature is provided.

Not all applications apparently understand this. Some file managers treat "NoDisplay" as "Hidden", and will then even strip out your application entry from mimeapps.list, as if mimeapps.list belonged only to them, and without telling you, as if you're a Windows user or something, and only paid for the OS. But it's not even correct behaviour.

Workarounds and Solutions

  1. Check your desktop entries for the applications of concern.
  2. Definitely remove any "Hidden=True" entries if the application is in use
  3. As a workaround, remove "NoDisplay=True" from your entry, applications may behave better
  4. Read the other answers to this question; I have not duplicated their content
  5. In extremis take a master copy of your preferred mimeapps.list or defaults.list file, and then delete all others, including mimeapps.cache

Clearly the Free Desktop specification might work a little better if applications actually used it, and also if they allowed users to use it for themselves, according to Linux principles, instead of overwriting mimeapps.list, either correctly or incorrectly, at a whim, without telling the user.

References

NeilG
  • 236