opscape

Files and directories

file

Guess what kind of data a file holds by looking at its contents, not its name.

file [OPTION...] FILE...

11 of 11 examples run in Debian 13.7 on 2026-10-31 · manual: file(1)

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
explain
output
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/*
explain
output
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.pdf
file report.pdf
explain
output
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
explain
output
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
explain
output
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
explain
output
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.txt
file notes.txt.gz
file -z notes.txt.gz
output
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.

A program binary

file -L /bin/ls | cut -c1-70
explain
output
/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
explain
output
      3 image/png

-b --mime-type prints only the type, so sort and uniq -c can count files per type. All three photos are PNGs.

Identify data from a pipe

head -c 64 photos/beach.png | file -
explain
output
/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

  • file reports what the bytes look like, not whether they are valid: a truncated PNG is still PNG 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 file program (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.

OptionWhat it does
-b, --briefDo not print the file name before the description.
-i, --mimePrint the MIME type and character set.
--mime-typePrint only the MIME type.
--mime-encodingPrint only the character set.
-L, --dereferenceFollow symbolic links.
-h, --no-dereferenceDescribe symbolic links themselves.
-z, --uncompressLook inside compressed files.
-f, --files-from FILERead the names to examine from FILE.
-0, --print0End each file name with a NUL byte in the output.
-s, --special-filesRead block and character devices too.
-k, --keep-goingReport every match, not just the first.
-EExit with an error status if a file cannot be read.

Related