Home·Terminal commands

file

Files and directories

Usage

file [path]

Works out a file's type from its contents rather than its name; it ignores the extension, which is how a .jpg that is really a PDF is caught.

Common flags

FlagMeaning
-bPrint only the description, without the file name
--mime-typePrint the MIME type; the short form is -i on GNU, -I on macOS
-LFollow symlinks
-zLook inside compressed files
-sRead block and character devices too
-iOn BSD and macOS this means do not classify contents, not MIME

Examples

file screenshot

Says PNG image data even with no extension.

file -b --mime-type report.pdf

Prints application/pdf and nothing else.

file *

Types every entry in this directory.

Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does file do?

Works out a file's type from its contents rather than its name; it ignores the extension, which is how a .jpg that is really a PDF is caught.

Q. How do I type it?

file [path] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

6 are listed here; the full set is in man file. This command sits under Files and directories.

Related commands

man page: man file