CIS120 Linux Fundamentals by Scott Shaper

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:

Basic Usage

The simplest way to use the file command is to type:

file filename

For example, if you have a file called notes.txt:

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

Let's check some common file types you might encounter:

file document.pdf image.jpg script.sh

Output:

document.pdf: PDF document, version 1.4
image.jpg: JPEG image data, JFIF standard 1.01
script.sh: Bourne-Again shell script, ASCII text executable

Using Brief Mode

If you just want to know the file type without the filename:

file -b notes.txt

Output:

ASCII text

Checking Web Files

For web development, you might want to see the MIME type:

file -i index.html style.css

Output:

index.html: text/html; charset=utf-8
style.css: text/css; charset=utf-8

Tips for Success

Common Mistakes to Avoid

Advanced Usage (For Later)

When you're more comfortable with Linux, you might want to try these advanced features: