0

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?

  • 8
    You're confusing gnome with linux. – bsd Mar 2 '16 at 9:54
  • I think you've got the nerve. Please elaborate it to an answer :D . – Shivam aggarwal Mar 2 '16 at 9:56
  • 2
  • Does this mean that changing the file extension changes it's MIME type ? – Shivam aggarwal Mar 2 '16 at 10:01
  • 3
    What @bsd is saying is that a desktop environment has its own mechanisms to determine which file to open with which application when selecting them in a file browser. And this mechanisms are usually extension based, for the convenience of the user and to follow the rule of least surprise. This has nothing to do with anything in Linux (meaning the kernel and, perhaps, the core libs and utilities), which remains extension agnostic. – Murphy Mar 2 '16 at 10:31
4

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.

  • 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 Mar 3 '16 at 8:27
  • 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 ? – Shivam aggarwal Mar 3 '16 at 8:52
  • @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 Mar 3 '16 at 10:36
  • @Shivamaggarwal As I explain in my answer, for applications Windows and Linux behave differently, but for data files they don't. – Gilles Mar 3 '16 at 10:37
  • 1
    @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). – Gilles Mar 3 '16 at 17:03
0

Desktop environments have their own mechanisms to determine which file to open with which application when selecting them in a file browser. This has nothing to do with anything in an underlying, unixoid operating system, in this case Linux (meaning the kernel and, perhaps, the core libs and utilities), which remains extension agnostic.

This mechanisms are usually extension based, for the convenience of the user and to follow the rule of least surprise. MIME types are just an abstraction used here to assign multiple extensions (e. g. .htm, .html, .HTML etc.) to a common content type, which is then used to determine what you want to/can do with a file. Thus in your desktop settings you can assign extensions to MIME types, and for each type select applications to open the files with.

However, when it comes to executable files it was in the olden times of DOS that there were only three types of files said OS was able execute, and it decided how they were interpreted by the extension: .bat, .com, .exe. This times are long gone, and other OS like Unix and all it descendants never relied on such a delicate mechanism but always had other ways to decide if and how to execute a file.

The convention of using extensions was found useful, expanded and is still used, but not for the purpose of the OS but for the convenience of users, which don't have to look into a file named something.sh to discover what it may contain. However, the OS does not rely on that, but applications may, and scripts do it all the time:

for file in *.html; do ...
  • But then 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 ? – Shivam aggarwal Mar 3 '16 at 8:52
  • @Shivamaggarwal I extended my answer. – Murphy Mar 3 '16 at 9:37
  • Thanks alot but sorry to say I'm feeling really confused now. Windows also doesn't need extensions that way I guess. Can you please use a example for explaining me the difference in using extensions with windows and using extensions in linux and then, not using extensions in both the environments. – Shivam aggarwal Mar 3 '16 at 11:06
  • @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. – Murphy Mar 3 '16 at 11:37
  • Please mention those assumptions so that things get more clear for me :) . – Shivam aggarwal Mar 3 '16 at 11:51
0

As other answered: it is up to the program (shell) to choose what program to use to opening a specific file.

But it is not only the extension that make the choice: at the beginning of files there is usually a signature, which identify the format. [Note this is common in nearly all binary programs, not only on unix derived formats]. The program file (and its database) is the main source to decipher the file type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.