file Command
The file command determines the type of files by examining their content and magic numbers. It's essential for identifying file formats regardless of their extensions or names.
Syntax
Description
The file command tests each argument in an attempt to classify it. It uses magic numbers, file headers, and content analysis to determine file types. Unlike relying on file extensions, it examines the actual file structure.
Key features:
- Identifies file types by content analysis
- Uses magic numbers and file signatures
- Works regardless of file extensions
- Supports hundreds of file formats
- Provides MIME type information
Common Options
| Option | Description |
|---|---|
-b |
Brief mode (don't prepend filenames) |
-i |
Output MIME type strings |
--mime-type |
Output only MIME type |
--mime-encoding |
Output only MIME encoding |
-L |
Follow symbolic links |
-h |
Don't follow symbolic links (default) |
-z |
Try to look inside compressed files |
-0 |
Output null character after each filename |
Examples
Check single file type
Output: document.pdf: PDF document, version 1.4
Check multiple files
Identifies types of all matching files
Brief output (no filenames)
Output: JPEG image data, JFIF standard 1.01
Get MIME type
Output: document.pdf: application/pdf
Get MIME encoding
Output: text.txt: us-ascii
Full MIME information
Output: script.py: text/x-python; charset=us-ascii
Follow symbolic links
Shows type of the target file, not the link itself
Check compressed files
Looks inside the compressed archive
Check executable files
Output: /bin/ls: ELF 64-bit LSB executable, x86-64
Identify unknown files
Helps identify files without extensions
Check all files in directory
Identifies types of all files in /etc directory
Pipe input to file command
Output: /dev/stdin: ASCII text
Check binary vs text files
Distinguishes between binary executables and text files
Common File Type Outputs
| File Type | Typical Output | MIME Type |
|---|---|---|
| Text file | ASCII text | text/plain |
| PDF document | PDF document, version 1.4 | application/pdf |
| JPEG image | JPEG image data, JFIF standard | image/jpeg |
| PNG image | PNG image data | image/png |
| ZIP archive | Zip archive data | application/zip |
| ELF executable | ELF 64-bit LSB executable | application/x-executable |
| Shell script | Bourne-Again shell script | text/x-shellscript |
| HTML file | HTML document text | text/html |
Common Use Cases
- File Recovery: Identify recovered files without extensions
- Security Analysis: Verify file types match their extensions
- System Administration: Audit file types in directories
- Web Development: Determine MIME types for HTTP headers
- Data Processing: Sort files by type for batch processing
- Forensics: Identify suspicious or hidden file types
Scripting Examples
Shell script examples using file command for automation