Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux file command

Reference for the Linux file command: identify file types by content, print MIME types with -i, follow links with -L and read names from a list.

file looks at a file and tells you what it is: text, a script, an image, an archive, an executable, a directory. It does not trust the file name. Instead it runs three kinds of test in order: file system tests (is it a directory, a symlink, empty?), "magic" tests (known byte patterns at known offsets), and language tests (is this plain text, and in which encoding?).

That makes it the first tool to reach for when a download has no extension, when a name lies, or when a script fails with "cannot execute binary file".

Terminal
file [OPTION]... FILE...

Try it

Bash Types of different files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash MIME types, links and encodings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
file [-bhiLz] [--mime-type] [--mime-encoding] [-f NAMEFILE] FILE...
Option Description
-b, --brief Omit the file name from the output.
-i, --mime Print the MIME type and charset (text/plain; charset=utf-8).
--mime-type Print only the MIME type.
--mime-encoding Print only the character encoding.
-L, --dereference Follow symbolic links.
-h, --no-dereference Do not follow symlinks (the default on most builds).
-f NAMEFILE Read the names to examine from NAMEFILE, one per line.
-z, --uncompress Look inside compressed files.
-s, --special-files Also read block or character special files.

Returns: 0 normally, even when the type is "data" (unknown). A non-zero status means the file could not be examined, for example because it does not exist; file still prints a message for it.

Usage notes

  • Output is a guess, not a guarantee. Treat it as a hint when debugging, not as a security check: a crafted file can imitate the start of another format.
  • "data" means no test matched. Many binary formats without a recognizable header end up here.
  • "ASCII text" versus "UTF-8 Unicode text": pure 7-bit text is reported as ASCII; any multi-byte UTF-8 sequence changes the answer. Latin-1 bytes are reported as "ISO-8859 text".
  • Scripts are identified from the #! line, and a missing execute bit does not change the description. Whether you can run it is a permission question, see stat.
  • Only the start is read. file samples the first part of the file, so a large file whose first kilobytes look like text but whose tail is binary may be misreported.
  • Empty files are reported as "empty", whatever their name says.
  • Use MIME output in scripts: file --mime-type -b "$f" gives short, stable strings such as text/plain and inode/directory, much easier to compare than the free-form description.
  • Compare with ls/stat: those tell you about metadata; file tells you about content.