On my system (Debian Jessie) I found all the normal methods of specifying a default application wouldn't override the one hiding in ~/.config/mimeapps.list
.
Thunar insisted on opening a particular file with gedit even after I told it to make the correct application the default. When I removed the setting from ~/.config/mimeapps.list
, it started working properly again.
UNIX/Linux does not have the same early DOS / CP/M heritage that Windows does. So extensions are generally less significant to most UNIX utilities and tools.
I usually use a command-line only environment. Extensions in such an environment under Linux aren't really significant except as a convenience to the operator or user. (I don't have enough experience with KDE or GNOME to know how their filemanagers deal with extensions.)
But such convenience is usually important. If config.ini
is really in Microsoft-standard ".ini" format, I'd let the extension stand. Plain old text files usually carry no extension in Linux, but this isn't universal for all programs configuration files. The programmer usually gets to decide that.
I think ".txt" is useful under Linux if you want to emphasize that it's NOT a configuration file or other machine-readable document. However, in source distributions, the convention is to name such files all caps without an extension (i.e. README, INSTALL, COPYING, etc.)
There are some standards and conventions but nothing stopping you from naming anything whatever you want, unless you are sharing things with others.
In Windows, naming a file .exe
indicates to the shell (usually explorer.exe
) that it's an executable file. UNIX builds this knowledge into the file system's permissions. If the proper x
bits (see man chmod
) are set, it is recognized as executable by shells and kernel functions (I believe). Beyond this, Linux doesn't care, most shells won't care, and most programs look in the file to find it's "type."
Of course, there's the nice command file
which can analyze the file and tell you what it is with a degree of certainty. I believe if it can't match the data in the file with any known type, and if it contains only printable ASCII/Unicode characters, then it assumes its a text file.
@Bruce Ediger below is absolutely correct. There is nothing in the kernel or filesystem level, i.e. Linux itself, enforcing or caring that the contents of a file needs to match up with its name, or the program that is supposed to understand it. This doesn't mean it's not possible to create a shell or launcher utility to do things based on filename.
Best Answer
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 startc:\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.