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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
From the wiki entry,
Linux-based GUIs, such as KDE and GNOME, support MIME type-based
associations. For example, the MIME type text/plain would be
associated with a text editor.
Also, this answer has more details.
File manager often use some combination of option a and b (e.g. look
at the file extension first, if it’s not known (or the file does not
have an extension), look at the contents).A file’s type is not stored as metadata on common linux file systems.
From this answer, I see the below information.
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.
Method 3
A file doesn’t tell the OS its file format, with one exception:
If an executable file starts with a so-called shebang line, like
#! /usr/bin/perl
then this line tells the Linux kernel that it should be started with /usr/bin/perl (that is, instead of executing this file, it should execute usr/bin/perl and give this file as argument). This of course only works with file formats which ignore (or understand) such a shebang line.
Other than that, the operating system as such knows nothing about file types. However many programs which allow opening other files have their own mechanism to decide which program to use for opening which file. That can be a list inside the program itself (Firefox for example maintains such a list), or the program can delegate it to the desktop environment (of which there are several for Linux).
Usually, those mechanisms only consider the filename to decide what type the file is; an exception are email clients and web browsers, where usually the type of the file is explicitly given by the server/stated in the mail.
The mechanism is usually two-step:
- First, from the file name or supplied information, determine the MIME type of the file (the MIME type is a standardized, but extensible list of file types).
- Second, from the MIME type, determine which program to execute, usually through a sort of registry maintained either by the program itself or by the desktop environment the program was written for.
Method 4
It is done similar to MS-Windows.
It is not done by OS. It is done by file manager, a part of the windowing system.
Usually the part of the file-name after the last dot . is used to make the
decision. There is a way to set them in settings, or when you right click on a file.
So the file does not tell the OS, the file is not active so can do nothing on its own, but the file manager looks at the file-name, the file manager could also look inside the file much as the file utility does.
Method 5
As @richard says, it’s similar to Windows. A generic file manager may look at the name of a file first and try to recognize a file extension which is associated with a program. If that fails, it may try to open the file and look for a header. If you try to read a non-text file (say, a .jpeg), you’ll see a string of data which describes the file. For instance, this is the first line of a .jpeg which I had lying around:
'xffxd8xffxe0x00x10JFIFx00x01x01x00x00x01x00x01x00x00xffxfex00;CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), quality = 90'
Method 6
I will attempt to give an underlying answer as to how Unix systems handle files. As others have pointed, your phrasing of the question is misleading since the files themselves do not report what type they are and the kernel itself doesn’t decide which applications to use.
In Unix and Windows operating systems, files are structured simply as a sequence of bytes. From Tanenbaum’s Operating Systems, third edition:
In effect, the operating system does not know or care what is in the file. All it sees are bytes. Any meaning must be imposed by user-level programs.
This is why, in a desktop environment, xdg-open is used to determine default applications for opening specific files types.
On a Unix system, files are differentiated between regular files and special files. A regular file is merely user data which may be either a text file (ASCII or Unicode encoding) or binary (executable code). Special files are files which are essential for running the OS. These include directories, links, pipes, sockets, and device files. ¹
It is by convention that file names contain extensions to indicate what type of file they may be e.g. *.c for C source code, *.html for web pages, etc. From Tanenbaum:
In some systems (e.g., UNIX), file extensions are just conventions and are not enforced by the operating system. A file named file.txt might be some kind of text file, but that name is more to remind the owner than to convey any actual information to the computer. On the other hand, a C compiler may actually insist that files it is to compile end in .c, and it may refuse to compile them if they do not.
If a file is saved or renamed without the proper extension, it still retains the same format for that file type. This is how most file managers decide what applications to open files with, and why without the extension most will prompt the user with “Choose the program you want to use to open this file:”.
Lastly, environment variables are used to define system-wide and user-level settings. For example, the variable EDITOR is used to specify which text editor to open when handling text e.g. vim, nano, etc. This is how processes know which editor to open, like when editing a git commit message. Otherwise, you are correct that you must specify which application to use. Note that vim detects file types by checking extensions and inspecting file contents, if the setting filetype on is set, which then applies the correct syntax highlighting, indentation, etc. ²
TL;DR
In summary, it is the user-level applications which determine which programs to open a specific file type with, and not the OS itself.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0