Session and utilities

The file command

Guesses a file's real type from its content, not from its extension.

Syntax

file archivo...Try it in the terminal →

Most useful options

-b, --briefDrops the file name at the start of the line, leaving only the type
-i, --mimeShows the MIME type, such as text/plain or image/png, instead of the description
-L, --dereferenceFollows symbolic links and analyzes the file they point to
-z, --uncompressLooks inside compressed files, such as .gz ones, to describe their content
-EOn a file that does not exist, prints the error and exits with a non-zero status
-F, --separator separadorChanges the text between the name and the type. The default is a colon

Copy-ready examples

$ printf 'hola\n' > f.txt && file f.txtcreates a text file and shows its type.

Example output

f.txt: ASCII text
$ file -i f.txtthe same file, with its MIME type.

Example output

f.txt: text/plain; charset=us-ascii
$ file *the type of every item in the current folder, all at once.

Example output

Documentos: directory
f.txt: ASCII text
$ file -L enlaceanalyzes the real file that link points to, not the link itself.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
noexiste: cannot open `noexiste' (No such file or directory)The name does not exist in the current folder, or it is misspelled.List the folder with ls and copy the exact name.
Usage: file [-bcCdEhikLlNnprsSvzZ0] [--apple] [--extension] [--mime-encoding]You ran file without any file name. What appears is the usage help.Pass the file name, such as file report.pdf, or file * for the whole folder.

Typical mistakes

  • The extension does not matter: a .txt that is really an image is detected as an image.
  • Without -L, file does not follow symbolic links: it describes the link, not the file it points to.
  • For a file that does not exist, file prints the error but exits with status 0. Use -E if the failure must count in a script.

Differences from similar commands

  • stat: describes how the file is stored: inode, permissions, dates. file says what content it has.
  • xxd: shows the raw bytes in hexadecimal so you can read them yourself. file interprets them and gives you the type.

Related commands

FAQ

What does file do with a file it does not recognize?

It checks whether it is text. If it is not text either, it describes it as data, meaning binary or of an unknown format.

How do I check many files at once?

Pass several names or a wildcard: file * gives the type of every item in the folder in one output.

What is the MIME type for?

Browsers and web servers use it to decide how to handle a file, for example in the Content-Type header.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course