file Command
Beginner File Viewing & Searching man(1)Determine the type of a file
👁 9 views
📅 Updated: Mar 15, 2026
SYNTAX
file [OPTION] FILE...
What Does file Do?
The file command determines the type of a file by examining its contents rather than its extension. It uses magic number databases, filesystem tests, and language tests to identify files.
file is essential because Linux does not rely on file extensions for type determination. A file named photo.jpg might actually be a text file, and a file without an extension could be a valid executable. file reads the actual content to make an accurate determination.
The command can identify hundreds of file types including text encodings, image formats, compressed archives, executables, scripts, documents, and more. It also detects MIME types useful for web servers and email handling.
file is essential because Linux does not rely on file extensions for type determination. A file named photo.jpg might actually be a text file, and a file without an extension could be a valid executable. file reads the actual content to make an accurate determination.
The command can identify hundreds of file types including text encodings, image formats, compressed archives, executables, scripts, documents, and more. It also detects MIME types useful for web servers and email handling.
Options & Flags
| Option | Description | Example |
|---|---|---|
| -i | Output MIME type instead of description | file -i document.pdf |
| -b | Brief mode - omit filename from output | file -b image.png |
| -z | Look inside compressed files | file -z archive.gz |
| -L | Follow symbolic links | file -L /usr/bin/python |
| -f | Read filenames from a file | file -f filelist.txt |
| -s | Read block/character special files | sudo file -s /dev/sda1 |
Practical Examples
#1 Identify file type
Determines the actual type of the file by examining its contents.
$ file report.pdf
Output:
report.pdf: PDF document, version 1.7
#2 Check MIME type
Shows the MIME type, useful for web servers and content negotiation.
$ file -i image.png
Output:
image.png: image/png; charset=binary
#3 Check multiple files
Identifies the type of every file in the current directory.
$ file *#4 Identify executable type
Shows whether a binary is 32/64-bit, statically/dynamically linked, etc.
$ file /usr/bin/ls
Output:
/usr/bin/ls: ELF 64-bit LSB pie executable, x86-64, dynamically linked
#5 Check encoding
Shows the character encoding of a text file (UTF-8, ASCII, etc.).
$ file -i data.csv
Output:
data.csv: text/plain; charset=utf-8
#6 Examine filesystem type
Identifies the filesystem on a partition.
$ sudo file -s /dev/sda1
Output:
/dev/sda1: Linux rev 1.0 ext4 filesystem
Tips & Best Practices
Verify before extracting: Always check archives with file before extracting. A file named archive.tar.gz might not actually be a gzip file.
MIME types for web servers: Use file -i to get MIME types when configuring web server content types or email attachments.
Not foolproof: file uses heuristics and can occasionally misidentify files. It reads file headers, not the entire content.
Frequently Asked Questions
How do I check the type of a file?
Use file filename. It examines the file's contents (not the extension) to determine the type.
How do I check text file encoding?
Use file -i filename. It shows the MIME type and character encoding (UTF-8, ASCII, ISO-8859-1, etc.).
Why does file show a different type than the extension?
file reads the actual file contents, not the extension. If a .jpg file shows 'ASCII text', it was likely renamed or corrupted.
Related Commands
More File Viewing & Searching Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →