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.gzOutput shows file type, encoding, and metadata.
Useful Options
MIME type
file -i myfile.txtShows MIME type (text/plain; charset=utf-8).
Examine inodes
file -s /dev/sda1Can examine device files.
Print raw magic bytes
file -r myfileShows raw magic database entry.
Common Examples
Check script encoding
file script.sh
script.sh: POSIX shell script, UTF-8 Unicode textIdentify binary type
file /bin/ls
/bin/ls: ELF 64-bit LSB executableDetect compression
file data.gz
data.gz: gzip compressed dataQuick Reference
| Flag | What It Does |
|---|---|
-i |
MIME type |
-z |
Compressed |
-b |
Brief |
-s |
Device files |
