Where to start First steps track · 15 stages
Linux Labs

file — determining file type

3 min Linux

Figuring out what's inside a file (text, ELF, archive, image) by content, not by extension.

Cheat sheet

4 commands Printable version
  • file f type by content
  • file -i f MIME + encoding
  • file -b f type only (for scripts)
  • file -s /dev/sda1 filesystem type on a device (as root)

Overview

file determines a file's type by its content (a signature, "magic bytes"), not by its extension. On Linux the extension means nothing — file tells the truth.

bash
file report           # report: ASCII text
file /bin/ls          # ELF 64-bit LSB pie executable, x86-64 …
file archive          # gzip compressed data  (even without a .gz extension)
file photo            # JPEG image data, …

file matches against a database of signatures (/etc/magic, the file/libmagic package) rather than guessing from the extension — renaming a file to .txt won't change the result.

Options

5

-b (brief, no filename), -i (MIME type and encoding), -L (follow a symlink and look at its target, not the link itself), -s (look inside special files/block devices — otherwise file just says "block special"), -z (look inside a compressed file instead of just identifying it's gzip).

Option What it does Example
-b --brief Don't print the filename, only the type — handy in scripts. file -b script.sh
-i --mime MIME type and encoding instead of a human-readable description. file -i notes.txt
-L --dereference Follow a symlink and inspect its target, not the link itself. file -L symlink
-s --special-files Look inside special files and block devices (otherwise just "block special"). file -s /dev/sda1
-z --uncompress Look inside a compressed file to determine the content type, not just the compression format. file -z archive.gz

Nothing found. Try a shorter query.

Recipes

1

Examples

bash
file *                        # types of every file in a directory
file -i notes.txt             # MIME type: text/plain; charset=utf-8
file -b script.sh             # no filename (type only) — handy in scripts
file -L symlink               # follow the symlink and look at the target
file -s /dev/sda1             # -s: look inside a block device (filesystem type)
file -z archive.gz            # look INSIDE a compressed file

Troubleshooting

4

"Script won't run: cannot execute binary file"

file script shows the real type. Common causes: it's HTML/an error page instead of a binary (the wrong thing got downloaded), or CRLF line endings (file will say "with CRLF line terminators") — fix it with sed -i 's/\r$//' script.

The text encoding is in question

file -i file shows the charset (utf-8 / iso-8859-1). Use iconv to re-encode it.

Determining the filesystem type on a partition

file -s /dev/sdXN (as root) is a quick way without mounting; for partitioning, see mount.

file says "data" instead of a specific type

No signature matched in the /etc/magic database — either the file is corrupted, or it's an unknown/raw format (for instance, encrypted data looks like a random byte stream). That's not a file error but an honest "couldn't identify it".

See also

  • files-basics — Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, terminal copy-paste.
  • stat — Full information about a file: size, permissions, owner, times (atime/mtime/ctime), inode.
  • tar — Creating and extracting tar/gz/xz archives; backing up directories.
Loading…