5

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?

Braiam
  • 35,991
Nidal
  • 8,956

2 Answers2

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).

HalosGhost
  • 4,790
3

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