file command in Linux: identify a file's type

Updated November 1, 2026

The file command identifies a file's type from its contents, not from the filename extension.

Syntax

file [options] file...

Common cases

file notes.txt     # ASCII text
file /bin/ls       # ELF 64-bit executable
file photo.jpg     # JPEG image data
file archive.gz    # gzip compressed data

Sample output

$ ls
notes.txt  photo.jpg
$ file notes.txt photo.jpg
notes.txt: ASCII text
photo.jpg: PNG image data, 48 x 48, 8-bit/color RGBA, non-interlaced
$ mv photo.jpg photo.txt
$ file photo.txt
photo.txt: PNG image data, 48 x 48, 8-bit/color RGBA, non-interlaced

How it works: magic numbers

In Linux the name extension means nothing: a file called data.txt may be an image. file looks at the first bytes (called the magic number) and matches them against a signature database: \x7fELF at the start means an ELF executable, \xff\xd8 means JPEG, \x1f\x8b means gzip. If no signature is recognised, file decides whether the data is text or binary.

Common mistakes

file spots Windows line endings, and that is the usual explanation for a strange startup error. file run.sh prints run.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators, while ./run.sh answers bash: ./run.sh: cannot execute: required file not found. What is missing is not the script but the interpreter: the #!/bin/bash line ends with a carriage return, and no file by that name exists.

On a symbolic link file describes the link, not what it points at. file link prints link: symbolic link to /etc/hostname. To get the type of the target, use file -L link, which prints link: ASCII text.

The word executable in the answer from file says nothing about permissions. file ok.sh prints ok.sh: Bourne-Again shell script, ASCII text executable only because the file starts with #!. Meanwhile ls -l ok.sh shows -rw-r--r-- and running it gives bash: ./ok.sh: Permission denied. The execute bit comes from chmod +x; file never looks at it.

Used in

You need this in the lesson Reading text. The theory is the chapter Reading and editing text.

See also

cat, head, tail, The ls command.

Try it

Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.