file determines the type of a file by examining its contents, not just its extension.
Synopsis
file [OPTIONS] FILE...
Common Options
| Option | Description |
|---|
-b | Brief (no filename) |
-i | MIME type |
-L | Follow symlinks |
-z | Look inside compressed files |
Examples
Basic usage
$ file /etc/passwd
/etc/passwd: ASCII text
$ file /bin/ls
/bin/ls: ELF 64-bit LSB pie executable, x86-64
$ file image.png
image.png: PNG image data, 1920 x 1080, 8-bit/color RGBA
Multiple files
$ file *
document.pdf: PDF document, version 1.4
image.jpg: JPEG image data, JFIF standard 1.01
script.sh: Bourne-Again shell script, ASCII text
data.bin: data
MIME type
$ file -i document.pdf
document.pdf: application/pdf; charset=binary
$ file -i script.sh
script.sh: text/x-shellscript; charset=us-ascii
Brief output
$ file -b /etc/hosts
ASCII text
Inside compressed files
$ file -z archive.tar.gz
archive.tar.gz: POSIX tar archive (gzip compressed data)
Common File Types
| Output | Meaning |
|---|
| ASCII text | Plain text file |
| UTF-8 Unicode text | Text with unicode |
| Bourne-Again shell script | Bash script |
| Python script | Python code |
| ELF 64-bit LSB executable | Linux binary |
| Mach-O 64-bit executable | macOS binary |
| PDF document | PDF file |
| PNG/JPEG image data | Image files |
| data | Unknown binary |
Common Patterns
Check if executable
if file "$1" | grep -q "executable"; then
echo "This is an executable"
fi
Find all images
Check file encoding
$ file -i textfile.txt
textfile.txt: text/plain; charset=utf-8
Tips
- Don’t trust extensions:
file checks content, not name - Binary vs text: Quick way to identify file nature
- Encoding issues: Use
-i to find charset - Magic database: Uses
/usr/share/misc/magic
See Also
- ls — List files
- stat — Detailed file information
Tutorials