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

file [OPTION]... [FILE]...

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

file document.pdf

Output: document.pdf: PDF document, version 1.4

Check multiple files

file *.txt *.jpg *.zip

Identifies types of all matching files

Brief output (no filenames)

file -b image.jpg

Output: JPEG image data, JFIF standard 1.01

Get MIME type

file --mime-type document.pdf

Output: document.pdf: application/pdf

Get MIME encoding

file --mime-encoding text.txt

Output: text.txt: us-ascii

Full MIME information

file -i script.py

Output: script.py: text/x-python; charset=us-ascii

Follow symbolic links

file -L symlink

Shows type of the target file, not the link itself

Check compressed files

file -z archive.tar.gz

Looks inside the compressed archive

Check executable files

file /bin/ls

Output: /bin/ls: ELF 64-bit LSB executable, x86-64

Identify unknown files

file mysterious_file

Helps identify files without extensions

Check all files in directory

file /etc/*

Identifies types of all files in /etc directory

Pipe input to file command

echo "Hello World" | file -

Output: /dev/stdin: ASCII text

Check binary vs text files

file program.exe script.sh data.bin

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

#!/bin/bash # Find all image files in directory for file in *; do if file "$file" | grep -q "image"; then echo "$file is an image" fi done # Sort files by type mkdir -p images documents archives for file in *; do type=$(file -b "$file") case "$type" in *image*) mv "$file" images/ ;; *PDF*|*text*) mv "$file" documents/ ;; *archive*|*compressed*) mv "$file" archives/ ;; esac done # Check if file is executable if file "$1" | grep -q "executable"; then echo "$1 is executable" else echo "$1 is not executable" fi

Shell script examples using file command for automation

See also