What it does
Linux does not need file endings: report can be a PDF and photo.jpg can be a script. file looks at the content (the first bytes, "magic numbers") and tells you the real type.
Use it before you open an unknown file, when you investigate a suspicious upload, or to see if a program is 32-bit, 64-bit or for ARM.
How to write it
file [OPTIONS] FILE...
file -C [-m MAGICFILES]Options 30
Most used
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1What is this file?
file mystery mystery: PDF document, version 1.7The name had no ending; the content says PDF.
2Check a program's architecture
file /usr/bin/ls /usr/bin/ls: ELF 64-bit LSB pie executable, x86-64, dynamically linked …A download for the wrong CPU (ARM vs x86-64) gives "Exec format error";
fileshows why.3Get the MIME type for a script
file -b --mime-type upload.jpg image/jpegWeb apps should check the real type of uploads, not the file name.
4Find a disguised script in an upload folder
file /var/www/uploads/* | grep -iv imageA "picture" that is really
PHP scriptis a typical web shell.5Check the text encoding
file -i data.csv data.csv: text/plain; charset=iso-8859-1Convert to UTF-8 with
iconvbefore an import.6Look inside a compressed file
file -z backup.sql.gzShows the type of the data inside the gzip file.
7Check what is on a disk partition
file -s /dev/vdb1 /dev/vdb1: SGI XFS filesystem data …Find the file system type of a partition (or "data" when it is empty).
8Check Windows line endings in a script
file deploy.sh deploy.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminatorsCRLF breaks bash scripts ("bad interpreter"). Fix it with
sed -i 's/\r$//' deploy.sh.9Check many files from a list
find /tmp -type f -newer /tmp/marker > list.txt; file -f list.txtClassify every new file in one run (incident response).
10See if a binary is static or dynamic
file /usr/local/bin/kubectl"statically linked" binaries do not need system libraries.
11Only the type, no name
file -b /etc/passwd ASCII textEasy to use in
iftests in scripts.
Nothing matches this filter.
Watch out
fileguesses from patterns; very short or special files can be reported wrongly.- Running
fileon hostile files is mostly safe (it uses a sandbox), but do not open them in other programs to "check".
Files
/usr/share/misc/magic.mgc- The compiled database of file patterns.
/etc/magic- Your own extra patterns.
Tips to remember
- The name lies, the content does not: use
file.