file
Command in Linux
Summary
The file
command is a standard Unix program used to determine the type of a file. It tests each argument in an attempt to categorize it. These tests check the results of file system calls, the magic number, and according to language-specific tests.
Introduction
The file
command is an essential tool for any Linux user. It allows you to quickly identify the type of a file, regardless of its extension. This is particularly useful when dealing with files that have no extension or have misleading extensions. file
command uses a database of magic numbers to identify file types. Magic numbers are specific byte sequences at the beginning of a file that identify its format.
Use case and Examples
Basic File Type Identification
This command will output the type ofmyfile.txt
. For example, it might return "myfile.txt: ASCII text" if the file contains plain text. Identifying an Executable File
This command will identify the/usr/bin/ls
executable. The output will likely be similar to "/usr/bin/ls: ELF 64-bit LSB shared object, x86-64...". ELF indicates that it's an Executable and Linkable Format, commonly used for executables on Linux. Checking a File with No Extension
If you have a file namedconfig
without an extension, the file
command can tell you what it is. It might be a text file, a shell script, or something else entirely. Identifying a Symbolic Link
Ifmylink
is a symbolic link, the output will indicate that it is a symbolic link and will also point to the linked file. For example: "mylink: symbolic link to actualfile.txt". Checking Multiple Files
You can provide multiple files as arguments tofile
. The command will then output the type of each file in sequence. Commonly used flags
Flag | Description | Example |
---|---|---|
-b | Brief mode; do not prepend filenames to output lines. | file -b myfile.txt |
-i | Output MIME type strings rather than more traditional human-readable ones. | file -i myfile.txt |
-f <file> | Read the names of the files to be examined from <file> . | file -f filelist.txt (where filelist.txt contains a list of files, one per line) |
-z | Try to look inside compressed files. | file -z archive.gz |
-L | Follow symbolic links. | file -L mylink |