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:
$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
$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
$file unknown_file
unknown_file: Bourne-Again shell script, ASCII text executable
$# Ah, it's a shell script!
Scenario 2: Wrong Extension
$file image.png
image.png: JPEG image data, JFIF standard 1.01
$# Someone named a JPEG with .png extension
Scenario 3: Security Check
$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:
$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
| Symbol | Type |
|---|
- | Regular file |
d | Directory |
l | Symbolic link |
c | Character device |
b | Block device |
s | Socket |
p | Named pipe (FIFO) |
Checking Multiple Files
$file *
config.json: ASCII text
directory/: directory
image.png: PNG image data, 800 x 600
script.sh: Bourne-Again shell script
Or recursively:
$file -r directory/
(all files in directory)
MIME Types
For web/programming contexts, use -i to get MIME types:
$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
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.