file — Determine File Type by Content
Detect the real file type by content instead of extension with file — via magic-number patterns, including MIME type, encoding and script use.
file determines a file's type not from its extension but from its actual content: it reads characteristic byte sequences at the start of the file – the so-called magic numbers – and matches them against a pattern database. This lets you reliably identify even misnamed or unnamed files, such as a PNG hiding behind a .dat extension or an executable ELF binary with no extension at all. For scripts, --mime-type emits a clean MIME type, -i adds the encoding, and -b omits the filename. That makes file the tool of choice before you open an unknown file or process an upload.
Basic Usage
file <file> — Identify the type of a file.
file document.pdffile <file1> <file2> — Identify multiple files at once.
file *.binfile -b <file> — Brief mode: omit the filename from output.
file -b image.pngfile * — Identify all files in the current directory.
file /usr/bin/*MIME Types
file -i <file> — Show MIME type and encoding.
file -i script.pyfile --mime-type <file> — Show only the MIME type.
file --mime-type photo.jpgfile --mime-encoding <file> — Show only the MIME encoding (charset).
file --mime-encoding data.csvfile -b --mime-type <file> — Show MIME type only, no filename (useful in scripts).
file -b --mime-type upload.datOptions
file -z <file> — Look inside compressed files.
file -z archive.gzfile -L <symlink> — Follow symbolic links.
file -L /usr/bin/pythonfile -s <device> — Read block/character special files (devices).
sudo file -s /dev/sda1file -f <list> — Read filenames from a list file (one per line).
file -f filelist.txtfile -k <file> — Keep going: show all matching types, not just the first.
file -k mystery.binSpecial Input
file - — Read from stdin and identify the content.
curl -s https://example.com | file -echo '<data>' | file - — Identify piped data.
echo 'PK' | file -file -b - < <file> — Identify redirected input (brief mode).
file -b - < unknown.datCommon Patterns
find . -type f -exec file {} + | grep 'text' — Find all text files recursively.
find /var/log -type f -exec file {} + | grep 'text'find . -type f -exec file {} + | grep 'ELF' — Find all binary executables.
find /usr/bin -type f -exec file {} + | grep 'ELF'file -i * | grep 'charset=utf-8' — Find all UTF-8 encoded files.
file -i *.txt | grep 'charset=utf-8'file * | grep -i image — Find all image files regardless of extension.
file uploads/* | grep -i imagefor f in *; do echo "$f: $(file -b "$f")"; done — Custom formatted file type listing.
for f in downloads/*; do echo "$f: $(file -b "$f")"; done Conclusion
file is the honest counterpart to the file extension: it looks at the content instead of the name and tells you what a file really is. For everyday use a bare file name is often enough; for scripts and pipelines -b --mime-type gives you a clean, machine-readable MIME type with no noise. Combined with find -exec you sweep entire directory trees for text, ELF binaries or images – regardless of how the files are named. Keep in mind, though: file guesses via heuristics and can be fooled, especially on very small or empty files. For pure metadata reach for stat, for a directory overview reach for ls, and to locate the files reach for find.
Further Reading
- file man page (man7.org) – complete reference for every option and exit status
- Wikipedia: file (command) – background on the command and the magic-number concept