4

This question already has an answer here:

In windows the OS know the type of file by using extension: exe,doc,ppt,pdf .... etc

In Linux as we know that the extension is useless.

My question is how can Linux know the type of file it is dealing with: shell script, audio file,video file .... etc or is it dealing with all the files in the same way?

marked as duplicate by strugee, jasonwryan, slm linux May 28 '14 at 0:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    @Andrew I hope that was a mispasted link... that question has nothing to do with this one. – strugee May 27 '14 at 23:36
  • @Stugee it was... – MathCubes May 27 '14 at 23:42
9

File extensions aren't useless for Linux distros, they are just a shortcut that is often uneeded. There are a couple of tools that Linux uses which make file extensions largely uneeded. The first, and perhaps the most obvious, is the shebang (#!).

The shebang is a line at the top of scripts executed by a shell to tell the shell what should be used to execute it. For example, the standard line to be included for a bash script is something like this:

#!/usr/bin/bash

This line tells the shell that the scripts contents should be executed by the utility located at /usr/bin/bash. However, shebangs are really only helpful for executable items.

The next tool used widely by modern applications are MIME types. Mime types are a declaration of file type used widely on the internet so that web browsers and email clients can know what type of file is being transferred. However, many programs rely on these types to know how to handle them (for example, X's .desktop files may include a mimetype line to declare a file-type association with a particular program).

Finally, a C library and command-line frontend were developed to detect mimetypes through heuristics. These are libmagic and file, respectively. Libmagic and file allow a program, or a user, to detect a file's mimetype even if it is not explicitly declared.


Admittedly, shebangs play a much smaller role in how modern operating systems determine the type of a file, but they are still widely used in the scripting world, and are certainly still a part of the equation.

Some of the declared filetype associations through mimetypes for X can be found in the file $HOME/.local/share/applications/mimeapps.list (for example).

2

Type

% man file

In essence, most file format designers reserve the first 4 characters or so to identify the format.

Those characters are know as the magic number of the file.

Keep in mind that unlike Windows or Mac, in Unix there is no file association.

The executable files are identified by some internal flags ('x').

The command

ls -l

shows you those flags.

  • 3
    Filetype associations definitely exist in *nix. They are just declared through mimetype associations (for example, in X .desktop files, as I mentioned) rather than at the system level. – HalosGhost May 27 '14 at 22:42

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