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.