Linux file Command: Identify Any File Type by Its Magic Number
The file command in Linux is used to determine the type of file passed as a parameter based on the Magic Number (the first two bytes).
Unlike Windows, file extensions mean nothing in Linux. The file command performs three types of checks to determine the type of file:
- File system test;
- Magic Number test;
- Language test
The first file system test is performed to determine whether the file is a regular file, a directory, a link, a device, a connection socket, etc. In Linux, absolutely everything is a file. The file type 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 specifies that the first two bytes of a file must contain a code indicating its type. For example, scripts start with the code "#!", followed by the full path of the interpreter that 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 language of the program.
Examples:
$ file teste1.cteste1.c: C source, UTF-8 Unicode text
$ file teste1teste1: 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 supermercado.xz supermercado.xz: XZ compressed data
$ file MAPA_MENTAL_LPI.pdfMAPA_MENTAL_LPI.pdf: PDF document, version 1.6
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.