kmail.at
contact

elsewhere

EnglishEN
← learning

linux · difficulty ◆

file — identify what a file really is

Trust the bytes, not the suffix.

That .jpg in your downloads folder is a PNG, and your deploy script is a Windows file. `file` says both in one line.

fully AI-generatedThis text is fully AI-generated and was published without human editorial review.

2026-10-19 · 4 min read

$ file

What it actually reads

Every format has a signature: ELF binaries start with \x7fELF, gzip with \x1f\x8b, PNG with an eight-byte header. `file` keeps thousands of those tests in a compiled table (/usr/lib/file/magic.mgc, 8.5 MB on my Debian box) and applies them, strongest test first. When nothing matches it falls back to sniffing text — ASCII, UTF-8, ISO-8859 — and finally to the one-word shrug `data`. It reads only the front of the file: a 2 GB file answered in 40 ms here, which is why running it across a whole tree is cheap.

Where it earns its keep

Triage: what did the client actually upload. Pre-deploy: is this binary x86-64 or arm64, and does it demand a glibc I don't have. Post-incident: the script that was fine in the editor and died on the server, because the editor saved CRLF. The `-i` variant is the one that writes code for you — MIME type plus charset, ready to compare against an allowlist instead of a filename.

Reading the answer line

`ELF 64-bit LSB pie executable, x86-64 ... interpreter /lib64/ld-linux-x86-64.so.2 ... stripped` is four answers at once. The architecture (copy an arm64 build here and the kernel answers `Exec format error`), PIE for a hardened build, the loader it expects, and `stripped` meaning a core dump will be bare addresses — the BuildID on that line is exactly what debuginfod needs to fetch symbols.

Example

$ file /usr/bin/ls
/usr/bin/ls: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=44900829b39271d878615772bb2a6ffa422f0891, for GNU/Linux 3.2.0, stripped

Run this before you scp a binary to a server. Arch and interpreter are on the first line; skip it and you get a confusing `Exec format error` instead.

$ cd ~/Downloads && file *
app.db:         SQLite 3.x database, last written using SQLite version 3045001, file counter 1, database pages 2, cookie 0x1, schema 4, UTF-8, version-valid-for 1
archive.txt.gz: gzip compressed data, was "access.log", last modified: Mon Oct 19 07:02:04 2026, from Unix, original size modulo 2^32 42780
blob.bin:       data
deploy.sh:      Bourne-Again shell script, ASCII text executable, with CRLF line terminators
empty.log:      empty
notes.txt:      ASCII text, with CRLF line terminators
photo.jpg:      PNG image data, 640 x 480, 8-bit/color RGB, non-interlaced
pic.png:        symbolic link to photo.jpg

photo.jpg is a PNG wearing the wrong extension, and deploy.sh is Windows-encoded. Both invisible in ls.

$ cd ~/Downloads && file -i *
app.db:         application/vnd.sqlite3; charset=binary
archive.txt.gz: application/gzip; charset=binary
blob.bin:       application/octet-stream; charset=binary
deploy.sh:      text/x-shellscript; charset=us-ascii
empty.log:      inode/x-empty; charset=binary
notes.txt:      text/plain; charset=us-ascii
photo.jpg:      image/png; charset=binary
pic.png:        inode/symlink; charset=binary

The shape an upload validator wants. Note inode/x-empty — a zero-byte file never matches an allowlist, which explains the mystery 415.

$ find . -type f | file -f -
./archive.txt.gz: gzip compressed data, was "access.log", last modified: Mon Oct 19 07:02:04 2026, from Unix, original size modulo 2^32 42780
./empty.log:      empty
./notes.txt:      ASCII text, with CRLF line terminators
./blob.bin:       data
./deploy.sh:      Bourne-Again shell script, ASCII text executable, with CRLF line terminators
./photo.jpg:      PNG image data, 640 x 480, 8-bit/color RGB, non-interlaced
./app.db:         SQLite 3.x database, last written using SQLite version 3045001, file counter 1, database pages 2, cookie 0x1, schema 4, UTF-8, version-valid-for 1

`-f -` takes names from stdin, so one file process identifies the whole tree instead of one exec per path.

$ file disk.img
disk.img: Linux rev 1.0 ext4 filesystem data, UUID=673f0ee4-417f-4c95-885a-8cf242c97ed6, volume name "BACKUP01" (extents) (64bit) (large files) (huge files)

An 8 MB image built with mke2fs. UUID and label come straight out of the superblock — instant proof that a backup file is a filesystem and not a failed dump. For a real device you need `file -s /dev/sdb1`.

$ ./deploy.sh
/usr/bin/bash: line 5: ./deploy.sh: cannot execute: required file not found

The file exists, chmod +x was done, and bash still says not found: the shebang is /bin/bash^M. `file deploy.sh` names it — CRLF line terminators — and `sed -i 's/\r$//' deploy.sh` fixes it. Next run prints deploy and exits 0.

Common flags

-b, --brief
Drop the filename prefix so the output is just the type. This is the form you use in `case $(file -b --mime-type "$f") in`.
-i, --mime
Print MIME type plus charset: `text/plain; charset=us-ascii`. The two values upload validation and charset bugs actually need.
-L, --dereference
Follow symlinks instead of reporting `symbolic link to photo.jpg`. Set automatically when POSIXLY_CORRECT is in the environment.
-z, --uncompress
Look inside gzip, bzip2, xz and zip containers and describe the inner format. `file -z backup.tar.gz` proves the tar is really in there.
-s, --special-files
Read block and character devices instead of stopping at `block special`. The only way to see which filesystem sits on /dev/sdb1.
-f, --files-from
Read names from a file instead of argv; `-f -` reads stdin, so a pipe gives one process for thousands of paths.
-E
Exit non-zero on filesystem errors (missing, unreadable) instead of printing the diagnostic and carrying on. Default is exit 0 — see the first fun fact.

History

November 1973, Research Version 4

The installed man page is blunt about it: there has been a `file` command in every UNIX since at least Research Version 4, and that man page is dated November 1973. The original kept its magic numbers inside the binary. System V made the significant change — an external list of magic types — which cost a little speed and bought the flexibility that lets you add your own formats today.

The 1986 clean-room rewrite

The code running on your box is Ian Darwin's rewrite, written without looking at anybody else's source, based on the System V behavior. Christos Zoulas has been collecting magic entries for it ever since, which is why the database is a community artifact with contributions reaching back to USENET: obscure camera formats get recognized, your in-house binary format gets the word `data`. Version 5.45 here reads a compiled 8.5 MB magic.mgc, but the entries behind it are still hand-written magic rules.

Fun facts

Pros & cons

pros

  • + Answers "what is this?" for thousands of formats with zero setup — it ships in the base image of every distro I've touched.
  • + Reads content, so it catches exactly the failures extensions create: a .jpg that's a PNG, a .csv that's Latin-1, a script that's Windows-encoded.
  • + `-i` gives MIME type and charset in one pass, and `-f -` makes it fast enough to audit a whole tree.

cons

  • − Its answer for anything unrecognized is the word `data`, with no hint about what to try next.
  • − Header-based, so polyglots and truncated downloads read as whatever the first bytes claim to be.
  • − Exits 0 even when the file doesn't exist unless you pass -E — a silent hole in `set -e` pipelines.

Takeaways

  1. 1Use `-b` when the output feeds a script: `case $(file -b --mime-type "$f") in image/*) ...`
  2. 2Branch on `file -b --mime-type`, never on the extension, in anything that touches user uploads.
  3. 3If a script dies with "cannot execute: required file not found" or the shebang shows ^M, run `file` on it before you touch permissions.
  4. 4Batch through `-f -`: one process for a thousand paths, no per-file exec cost.
  5. 5Add `-E` in automation, or a missing path passes silently with exit 0.

Related commands

archive entry — written and published by a scheduled job, never editorially reviewed. Kept for reference: out of the site’s search index, and no ads run here. the reviewed library →

← all learning