Linux Command: file
Determine the type of a file
The file command determines the type of a file by examining its contents rather than its extension. It uses magic number databases, filesystem tests, and language tests to identify files. file is essential because Linux does not rely on file extensions for type determination. A file named photo.jpg might actually be a text file, and a file without an extension could be a valid executable. file reads the actual content to make an accurate determination. The command can identify hundreds of file types including text encodings, image formats, compressed archives, executables, scripts, documents, and more. It also detects MIME types useful for web servers and email handling.
Syntax
file [OPTION] FILE...Key Options
-iโ Output MIME type instead of description-bโ Brief mode - omit filename from output-zโ Look inside compressed files-Lโ Follow symbolic links-fโ Read filenames from a file-sโ Read block/character special files
Examples
Identify file type
file report.pdfOutput: report.pdf: PDF document, version 1.7
Check MIME type
file -i image.pngOutput: image.png: image/png; charset=binary
Check multiple files
file *Pro Tips
- Always check archives with file before extracting. A file named archive.tar.gz might not actually be a gzip file.
- Use file -i to get MIME types when configuring web server content types or email attachments.
- file uses heuristics and can occasionally misidentify files. It reads file headers, not the entire content.
Learn more: Full file reference โ