The Linux file Command: Determine File Types

The Linux file Command: Determine File Types

Linux determines file types by analyzing content, not extensions. The file command reads file headers and magic bytes to identify formats.


Basic Usage

file myfile.txt
file image.png
file archive.tar.gz

Output shows file type, encoding, and metadata.


Useful Options

MIME type

file -i myfile.txt

Shows MIME type (text/plain; charset=utf-8).

Examine inodes

file -s /dev/sda1

Can examine device files.

file -r myfile

Shows raw magic database entry.


Common Examples

Check script encoding

file script.sh
script.sh: POSIX shell script, UTF-8 Unicode text

Identify binary type

file /bin/ls
/bin/ls: ELF 64-bit LSB executable

Detect compression

file data.gz
data.gz: gzip compressed data

Quick Reference

Flag What It Does
-i MIME type
-z Compressed
-b Brief
-s Device files

Further Reading