Skip to content

File command on Linux (check file type)

The file command in Linux is used to determine the type of file entered as a parameter based on the Magic Number (first two bytes).

Unlike Windows, file extensions mean nothing in Linux. The file command performs three types of checks to determine the file type:

  • File system testing;
  • Magic Number testing;
  • Language testing

The first file system test is done to determine whether the file is an ordinary file, a directory, a link, a device, a connection socket, etc. In Linux, absolutely everything is a file. The type of file in the file system determines whether it is a regular file or another special type.

The second Magic Number test checks the first two bytes of the file to determine its type. There is a convention in computing that states that the first two bytes of the file must contain a code that indicates its type. For example, scripts start with the code “#!” , followed by the full path of the interpreter who will interpret and execute the script.

The third test, once it has been determined that the file is a program, script, or source code, indicates the program’s language.

Examples:

$ file test1.c test1.c: C source, UTF-8 Unicode text
$ file test1 test1: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, buildID [sha1] =f63857e5ce1a259a5228b1ea15ced24867583793, not stripped
$ file backup.sh backup.sh: Bourne-Again shell script, ASCII text executable
$ file supermarket.xz supermarket.xz: XZ compressed data
$ file MAPA_MENTAL_LPI.pdf MAPA_MENTAL_LPI.pdf: PDF document, version 1.6

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share