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.
There are commands that are similar to the double-click: xdg-open
is a fairly standard command that is shipped with many linux GUIs. On Debian distros, there is also see
and open
.
It would be good to point out that extensions are actually kind of arbitrary. There are so many esoteric extensions out there; no program, including the "double-click" can possibly know how to interpret every file out there.
And if you know the contents of file and the right program to run it, you should be able to execute/use the file regardless of its extension.
Best Answer
There is already an external command for this. There is nothing new that needs to be written. The command is
xdg-open
. It will open a file based on its MIME type association. Here is an example: