File Types

In Linux, extensions are optional. A file without .txt could still be text. A file with .jpg might not be an image.

So how do you know what you're dealing with?

The file Command

file examines the actual content, not the name:

Terminal
$file document.txt
document.txt: UTF-8 Unicode text
$file photo.jpg
photo.jpg: JPEG image data, JFIF standard 1.01
$file script.sh
script.sh: Bourne-Again shell script, ASCII text executable
$file program
program: ELF 64-bit LSB executable, x86-64, version 1 (SYSV)...

file uses "magic numbers" - special bytes at the start of files that identify their type.

Common File Types

Terminal
$file /etc/passwd
/etc/passwd: ASCII text
$file /bin/ls
/bin/ls: ELF 64-bit LSB pie executable, x86-64, ...
$file /etc/nginx/nginx.conf
/etc/nginx/nginx.conf: ASCII text
$file archive.tar.gz
archive.tar.gz: gzip compressed data, ...
$file document.pdf
document.pdf: PDF document, version 1.4

Why This Matters

Scenario 1: Mystery File

Terminal
$ls
unknown_file
$file unknown_file
unknown_file: Bourne-Again shell script, ASCII text executable
$# Ah, it's a shell script!

Scenario 2: Wrong Extension

Terminal
$file image.png
image.png: JPEG image data, JFIF standard 1.01
$# Someone named a JPEG with .png extension

Scenario 3: Security Check

Terminal
$file downloaded_file.txt
downloaded_file.txt: ELF 64-bit LSB executable
$# SUSPICIOUS - executable disguised as text file!

Don't Trust Extensions

Malware often uses fake extensions. Always check with file before running unknown files.

File Types in ls

Remember ls -l? The first character shows the type:

Terminal
$ls -l
-rw-r--r-- 1 user user 1024 Jan 14 regular_file.txt drwxr-xr-x 2 user user 4096 Jan 14 directory/ lrwxrwxrwx 1 user user 11 Jan 14 link -> target
SymbolType
-Regular file
dDirectory
lSymbolic link
cCharacter device
bBlock device
sSocket
pNamed pipe (FIFO)

Checking Multiple Files

Terminal
$file *
config.json: ASCII text directory/: directory image.png: PNG image data, 800 x 600 script.sh: Bourne-Again shell script

Or recursively:

Terminal
$file -r directory/
(all files in directory)

MIME Types

For web/programming contexts, use -i to get MIME types:

Terminal
$file -i document.txt
document.txt: text/plain; charset=utf-8
$file -i photo.jpg
photo.jpg: image/jpeg; charset=binary
$file -i script.sh
script.sh: text/x-shellscript; charset=us-ascii
Knowledge Check

How does Linux actually determine what type a file is?

Key Takeaways

  • file examines content, not extensions
  • Extensions are just conventions - they can be wrong
  • Always verify unknown files before running them
  • Use file -i for MIME types
  • ls -l shows file type in the first character

Next: wildcards for matching multiple files.