file – determine file type

file command
file command

file determines the type of a file by examining its contents, not just its extension.

Synopsis

file [OPTIONS] FILE...

Common Options

OptionDescription
-bBrief (no filename)
-iMIME type
-LFollow symlinks
-zLook inside compressed files

Examples

Basic usage

$ file /etc/passwd
/etc/passwd: ASCII text

$ file /bin/ls
/bin/ls: ELF 64-bit LSB pie executable, x86-64

$ file image.png
image.png: PNG image data, 1920 x 1080, 8-bit/color RGBA

Multiple files

$ file *
document.pdf:  PDF document, version 1.4
image.jpg:     JPEG image data, JFIF standard 1.01
script.sh:     Bourne-Again shell script, ASCII text
data.bin:      data

MIME type

$ file -i document.pdf
document.pdf: application/pdf; charset=binary

$ file -i script.sh
script.sh: text/x-shellscript; charset=us-ascii

Brief output

$ file -b /etc/hosts
ASCII text

Inside compressed files

$ file -z archive.tar.gz
archive.tar.gz: POSIX tar archive (gzip compressed data)

Common File Types

OutputMeaning
ASCII textPlain text file
UTF-8 Unicode textText with unicode
Bourne-Again shell scriptBash script
Python scriptPython code
ELF 64-bit LSB executableLinux binary
Mach-O 64-bit executablemacOS binary
PDF documentPDF file
PNG/JPEG image dataImage files
dataUnknown binary

Common Patterns

Check if executable

if file "$1" | grep -q "executable"; then
    echo "This is an executable"
fi

Find all images

$ file * | grep -i image

Check file encoding

$ file -i textfile.txt
textfile.txt: text/plain; charset=utf-8

Tips

  • Don’t trust extensions: file checks content, not name
  • Binary vs text: Quick way to identify file nature
  • Encoding issues: Use -i to find charset
  • Magic database: Uses /usr/share/misc/magic

See Also

  • ls — List files
  • stat — Detailed file information

Tutorials