Why does linux use file extension to decide the default program for opening a file though it's independent of file extensions

18,832

Linux doesn't use file extensions to decide how to open a file, but Linux uses file extensions to decide how to open a file.

The problem here is that “Linux” can designate different parts of the operating system, and “opening a file” can mean different things too.

A difference between Linux and Windows is how they treat application files vs data files. On Windows, the line between the two is blurred; there are a few types of executable files, and they are determined by their extension (.exe, .bat, etc.), but in most contexts you can “execute” any file (e.g. by clicking in Explorer), and this executes the executable that is associated with that file type, where the file type is entirely determined by the extension (so executing a .doc file might start c:\Program Files\something or other\winword.exe, executing a .py file might start a Python interpreter, etc.).

On Linux, there is a notion of executable file which is independent of the file name. Executables generally have no extension, because they're meant to be typed by the user. The type of the file is irrelevant, all the user wants to do is execute the file. The kernel determines how to execute the file from the file contents: it knows some file types natively, and the shebang mechanism allows a file to declare any other executable file¹ as its interpreter.

On the other hand, data files usually do have an extension that indicates the type of data. The general idea here is that the type of data is not synonymous with what application to use to open the file with. You may want to view a PDF in Okular, or in Evince, or in Xpdf, or in Acroread, or in Mupdf, etc.

There are many tools that do however allow opening a data file without having to explicitly specify what application to use. These tools almost exclusively base their decision on the file extension. The file extension and the file's content are the only information that these tools have at their disposal: Linux does not store any meta information regarding the file format. So when you click on a .pdf file in a file manager (or when you run the .pdf file on a suitably-configured zsh command line, etc.), the file manager consults a database to find what application is the preferred one for .pdf file. This database may be structured in two sections, one that associates extensions to MIME types (/etc/mime.types, ~/.local/share/mime) and one that associates MIME types to applications (/etc/mailcap, ~/.local/share/applications), but even so the origin is the extension. While it would often be possible to figure out the application from the file content, this would be slower, and not always possible (many formats look just like text files, a .jar is a type of .zip, etc.).

Linux doesn't need file extensions, and it doesn't use them to determine how to run an executable file, but it does use them to determine which program to use to open a data file.

¹ That file has to be a native executable, a shebang executable can't point to another shebang executable to avoid potentially unending recursion.

Share:
18,832

Related videos on Youtube

Shivam Aggarwal
Author by

Shivam Aggarwal

Updated on September 18, 2022

Comments

  • Shivam Aggarwal
    Shivam Aggarwal almost 2 years

    I have a text file as- abc.text and it has its contents as Hi I'm a text file.

    If I double click to open this file, then the files is opened in gedit editor.

    Whereas, if I rename the file to abc.html (without changing any of its contents) then by default it opens in Chrome.

    This sort of behavior is acceptable on a Windows machine, since Windows uses file extensions to identify file types. But as far as I've read, Linux doesn't need file extensions.

    So why does changing file extensions in Linux changes the default program that opens it?

  • Shivam Aggarwal
    Shivam Aggarwal over 8 years
    This means that the responsibility of changing the default program for a file is on the Window Manager. Btw what is the meaning of a file being executable in linux ? Is a script file(*.sh) only considered as an executable file in linux.
  • Shivam Aggarwal
    Shivam Aggarwal over 8 years
    Also what is the use of not using or depending on extensions in linux. It exhibits similar behaviour to windows ? If I rename a image file to text file and open it with a image viewer then it still opens it ? Linux does the same. Where's the difference ?
  • Murphy
    Murphy over 8 years
    @Shivamaggarwal I extended my answer.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 8 years
    @Shivamaggarwal Not the window manager. There are several coexisting mechanisms, but the shared-mime-info framework specified by Freedesktop is winning. A file is executable if it has the execute permission (it's one of the three permission types, with read and write).
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 8 years
    @Shivamaggarwal As I explain in my answer, for applications Windows and Linux behave differently, but for data files they don't.
  • Shivam Aggarwal
    Shivam Aggarwal over 8 years
    Firstly thanx alot for being so patient with me and my stupid questions. Now, I just changed the permission of a text file by using chmod and if I now do a ls -l then it shows that it is a executable file. So just by changing one bit I can make a file executable :0 . This seems very weird. What is the general meaning of executable file in linux, because any kind of file can be made executable just by changing one of its bit.
  • Murphy
    Murphy over 8 years
    @Shivamaggarwal I doubt this would help. You are using wrong assumptions when looking at file extensions. Discard them. Then reread the answers you've already been given.
  • Shivam Aggarwal
    Shivam Aggarwal over 8 years
    Please mention those assumptions so that things get more clear for me :) .
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 8 years
    @Shivamaggarwal An executable file is one that has the execute permission, so it can be called as a command. Any kind of file can be given the execution permission. Whether this produces useful results or not depends on the file content (and of course on the system as well, e.g. an OSX program won't run on a Linux system).