file Command
Think of the file command as a detective that examines files to tell you what they really are. Unlike Windows or macOS, Linux doesn't rely on file extensions (like .txt or .jpg) to know what type of file something is. Instead, it looks at the actual content of the file to determine its type.
Why Use the file Command?
You might need the file command when:
- You download a file and don't know what it is
- You find a file without an extension
- You want to verify a file's type before opening it
- You're working with files from different operating systems
Basic Usage
The simplest way to use the file command is to type:
file filename
After running the course setup, try it on a file in ~/playground/chapter2. For example:
cd ~/playground/chapter2
file notes.txt
Output:
notes.txt: ASCII text
Common Options
Here are the most useful options you'll need as a beginner:
| Option | What It Does | When to Use It |
|---|---|---|
-b |
Shows only the file type (no filename) | When you want cleaner output |
-i |
Shows the MIME type | When working with web files |
-z |
Looks inside compressed files | When checking zip or tar files |
Practical Examples
Checking Different File Types
The course setup creates several file types in ~/playground/chapter2. Try checking a few at once:
cd ~/playground/chapter2
file notes.txt sample.sh index.html style.css readme empty_file sample.pdf sample.jpg
You should see output like:
notes.txt: ASCII text
sample.sh: Bourne-Again shell script, ASCII text executable
index.html: HTML document, ASCII text
style.css: ASCII text
readme: ASCII text
empty_file: empty
sample.pdf: PDF document, version 1.4
sample.jpg: JPEG image data, JFIF standard 1.01
Notice that readme has no extension, but file still identifies it as text by looking at its content. The empty_file is reported as "empty" because it has no content. The sample.pdf and sample.jpg files are minimal examples (they contain only the magic bytes that identify the format); they are not real documents or images, but file correctly reports their type.
Using Brief Mode
If you just want the file type without the filename, use -b:
cd ~/playground/chapter2
file -b notes.txt
file -b sample.sh
Output:
ASCII text
Bourne-Again shell script, ASCII text executable
Checking Web Files (MIME Type)
For web files, use -i to see the MIME type. The setup provides index.html and style.css in ~/playground/chapter2:
cd ~/playground/chapter2
file -i index.html style.css
Output:
index.html: text/html; charset=us-ascii
style.css: text/css; charset=us-ascii
Tips for Success
- Start Simple: Begin with basic file checks before using options
- Check Unknown Files: Always use
fileon files you're not sure about - Use Brief Mode: The
-boption gives cleaner output - Practice: Try checking different types of files to learn the output formats
Common Mistakes to Avoid
- Forgetting that Linux doesn't rely on file extensions
- Not checking file types before opening unknown files
- Getting confused between similar file types
- Not using the
-boption when you want cleaner output
Advanced Usage (For Later)
When you're more comfortable with Linux, you might want to try these advanced features:
- Using
-fto read filenames from a file - Using
-Lto follow symbolic links - Using
-sto read special files