Quick take: The file command identifies a file's type by inspecting its contents, not its name. file document tells you whether it is text, a JPEG, a script, or a binary — regardless of the extension.
Introduction
The file command determines a file's type by examining its actual content — magic numbers, structure, and encoding — rather than trusting the extension. This makes it the reliable way to tell what a file really is, whether you are handling an unknown download or a file with a misleading name.
Syntax
The basic syntax of the file command is:
file [OPTIONS] FILE...Common Options and Parameters
The most useful options and parameters for the file command:
| Option | Description |
|---|---|
| (default) | Describe the file's type in plain language. |
| -i | Show the MIME type (e.g. text/plain). |
| -b | Brief — omit the filename from the output. |
| -z | Look inside compressed files. |
| -L | Follow symbolic links. |
| -s | Read block or character special files (devices). |
Practical Examples
Real file commands you can run today:
# Identify a file's type
file mystery.dat
# Identify several files
file *
# Show the MIME type
file -i photo
# Brief output without the name
file -b script
# Look inside a compressed file
file -z archive.gz
# Check a block device's filesystem
sudo file -s /dev/sda1Tips and Best Practices
- Never trust an extension —
filereads the content, so it catches a JPEG named.txtor a script with no extension. - Use
file -ifor the MIME type when a script needs to branch on file type. sudo file -s /dev/sdXreports the filesystem on a block device, handy before mounting.
Final Thoughts
file answers “what is this, really?” by inspecting content instead of the name. Reach for it with unknown downloads, extensionless files, or anything with a suspicious name, and use -i for the MIME type in scripts. It is a small command that prevents a lot of confusion.
FAQ: file Command in Linux
How do I find out a file's type in Linux?+
Run file filename. It inspects the content and reports the type — text, image, executable, archive, and so on — regardless of the file's extension.
Does the file command use the extension?+
No. file examines the actual content (magic numbers and structure), so it correctly identifies a file even if the extension is wrong or missing.
How do I get the MIME type of a file?+
Use file -i filename, which prints the MIME type such as text/plain or image/jpeg, useful for scripts that branch on type.
How do I check the filesystem on a disk with file?+
Run sudo file -s /dev/sdX1. The -s flag lets file read special device files and report the filesystem they contain.
How do I identify many files at once?+
Pass multiple names or a glob: file * describes every file in the current directory in one go.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support