Files and directories
file
Guess what kind of data a file holds by looking at its contents, not its name.
file [OPTION...] FILE...
file reads the first bytes of each file and compares them with a database of known
signatures ("magic numbers"): PNG images start with \x89PNG, gzip with \x1f\x8b, ELF
programs with \x7fELF, and so on. For text it goes further and reports the character set, line
endings, and sometimes the language of a script from its #! line.
The extension plays no part, which is the point: file tells you what a download, an
attachment or a file with no extension really is. -b drops the file name, --mime-type gives a
MIME type for scripts and web servers.
It is a guess, though a good one. Short or unusual files can be misclassified.
Examples
Identify a few files
file notes.txt data/config.json photos/beach.png scripts/backup.sh
notes.txt: ASCII text data/config.json: JSON text data photos/beach.png: PNG image data, 640 x 480, 8-bit/color RGB, non-interlaced scripts/backup.sh: Bourne-Again shell script, ASCII text executable
The PNG is recognized with its dimensions and color type, JSON is detected from its
structure, and the script from its #! line.
Every file in a directory
file src/*
src/Makefile: ASCII text src/app.py: Python script, ASCII text executable src/lib: directory src/main.c: C source, ASCII text src/util.sh: Bourne-Again shell script, ASCII text executable
lib is a directory. app.py and util.sh are identified by their #! lines; main.c by
its contents.
The extension does not matter
Setup run before this example
cp photos/forest.png report.pdffile report.pdf
report.pdf: PNG image data, 800 x 600, 8-bit/color RGB, non-interlaced
The file is named .pdf but its bytes are a PNG image, and that is what file reports.
Spot Windows line endings
file data/windows.txt data/poem.txt
data/windows.txt: ASCII text, with CRLF line terminators data/poem.txt: ASCII text
with CRLF line terminators explains why a script or diff misbehaves on a file that came
from Windows. Fix it with sed -i 's/\r$//' or dos2unix.
Just the description
file -b photos/IMG_2041.png
PNG image data, 320 x 240, 8-bit/color RGB, non-interlaced
-b (brief) omits the name: prefix, which is handy in scripts.
MIME type for scripts and servers
file --mime-type data/users.csv data/config.json photos/beach.png
data/users.csv: text/csv data/config.json: application/json photos/beach.png: image/png
--mime-type prints a standard type such as image/png. --mime (or -i) adds the
character set.
Look inside compressed files
Setup run before this example
gzip -k notes.txtfile notes.txt.gz
file -z notes.txt.gz
notes.txt.gz: gzip compressed data, was "notes.txt", last modified: Sun Oct 25 05:30:44 2026, from Unix, original size modulo 2^32 190 notes.txt.gz: ASCII text (gzip compressed data, was "notes.txt", last modified: Sun Oct 25 05:30:44 2026, from Unix)
Without options, file identifies the gzip container and its header fields. -z decompresses
and describes what is inside.
Symbolic links
file link-to-notes
file -L link-to-notes
link-to-notes: symbolic link to notes.txt link-to-notes: ASCII text
By default file reports the link itself. -L follows it and describes the target.
A program binary
file -L /bin/ls | cut -c1-70
/bin/ls: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dyna
An ELF executable, with its architecture. The line is long (build ID, linking details), so
cut keeps the first 70 characters.
Identify files listed by find
find . -name '*.png' -exec file -b --mime-type {} + | sort | uniq -c
3 image/png
Identify data from a pipe
head -c 64 photos/beach.png | file -
/dev/stdin: PNG image data, 640 x 480, 8-bit/color RGB, non-interlaced
- makes file read standard input. The first 64 bytes are enough for the PNG signature
and header.
Common pitfalls
filereports what the bytes look like, not whether they are valid: a truncated PNG is stillPNG image data.Text encodings are guesses from the bytes present. A file that is pure ASCII is reported as ASCII even if it is meant to be UTF-8.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
Linux distributions and macOS both ship the same
fileprogram (from the Fine Free File command project), but versions and magic databases differ, so descriptions can vary slightly.
Options worth knowing
A selection, described in our own words. For the full list, see file(1) or file --help.
| Option | What it does |
|---|---|
-b, --brief | Do not print the file name before the description. |
-i, --mime | Print the MIME type and character set. |
--mime-type | Print only the MIME type. |
--mime-encoding | Print only the character set. |
-L, --dereference | Follow symbolic links. |
-h, --no-dereference | Describe symbolic links themselves. |
-z, --uncompress | Look inside compressed files. |
-f, --files-from FILE | Read the names to examine from FILE. |
-0, --print0 | End each file name with a NUL byte in the output. |
-s, --special-files | Read block and character devices too. |
-k, --keep-going | Report every match, not just the first. |
-E | Exit with an error status if a file cannot be read. |
Related
statShow a file's metadata (size, permissions, owner, inode, timestamps), or print chosen fields in your own format.lsList the contents of directories, or the details of individual files.xxdMake a hex dump of a file, or turn a hex dump back into binary with -r.odDump a file's bytes as octal, hex, decimal or characters, to see exactly what it contains.findWalk a directory tree and print, or act on, every file that matches a set of tests.