Linux File Opening – How Does Linux Choose Which Application to Open a File?

file openingfiles

If a file tells the OS its file format, how does the OS choose which application to open it by default?

In Windows, is the association stored in the registry table?

How does Linux choose which application to open a file?

I used to use Nautilus a lot, but now I change to terminal. Is it true that in terminal, we always have to explicitly specify which application to open a file? Does the settings of which application to open a file of a certain format by default belong to the file manager (e.g. Nautilus), and it is not an issue when we are living in terminals?

Best Answer

There may be different mechanisms to handle these default settings. However, other answers tend to focus on complete desktop environments, each of them with its own mechanism. Yet, these are not always installed on a system (I use OpenBox a lot), and in this case, tools such as xdg-open may be used.

Quoting the Arch Wiki:

xdg-open is a desktop-independent tool for configuring the default applications of a user. Many applications invoke the xdg-open command internally.

At this moment, I am using Ubuntu (12.04) and xdg-open is available. However, when you use a complete desktop environment such as GNOME, xdg-open acts as a simple forwarder, and relays the file requests to your DE, which is then free to handle it as it wants (see other answers for GNOME and Nautilus, for instance).

Inside a desktop environment (e.g. GNOME, KDE, or Xfce), xdg-open simply passes the arguments to that desktop environment's file-opener application (gvfs-open, kde-open, or exo-open, respectively), which means that the associations are left up to the desktop environment.

... which brings you back to the other answers in that case. Still, since this is Unix & Linux, and not Ask Ubuntu:

When no desktop environment is detected (for example when one runs a standalone window manager, e.g. Openbox), xdg-open will use its own configuration files.

All in all:

                            |-- no desktop env. > handle directly.
User Request > xdg-open > --|
                            |-- desktop env.    > pass information to the DE.

If the first case, you'll need to configure xdg-open directly, using the xdg-mime command (which will also allow you to see which application is supposed to handle which file). In the second case...

                        |-- GNOME? > gvfs-open handles the request.
                        |
Info. from xdg-open > --|-- KDE?   > kde-open handles the request.
                        |
                        |-- XFCE?  > exo-open handles the request.

... you'll need to configure the file-opener associated with your desktop environment. In some cases, configuration made through xdg-mime may be redirected to the proper configuration tool in your environment.

Related Question