Determining File Types

LinuxBeginner
Practice Now

Introduction

In Windows or macOS, you are used to file extensions telling you what a file is. If it ends in .jpg, it's a picture. If it ends in .exe, it's a program.

In Linux, however, filenames are just labels. And just like in real life, labels can be wrong. You can take a jar of salt and write "Sugar" on the label, but it will still taste salty.

Linux does not trust the label (the filename). Instead, it examines the contents to see what it really is.

This reflects a broader Unix idea: meaning often comes from how data is structured and used, not just from its name. File extensions can be convenient hints for humans, but they are not the final source of truth.

In this lab, you will use the file command to specific the true nature of files, regardless of what they are named.

That skill matters in real operations work. You may encounter downloaded files, logs, dumps, scripts, or suspicious attachments with misleading names. Before opening, executing, or editing them, it is wise to identify what they really are.

Check Text File

Let's start with a file that is exactly what it claims to be.

First, navigate to your project directory:

cd ~/project
ls -l

You see welcome.txt. The label .txt suggests it is a text file. But remember, we don't trust labels.

Ask the file command to inspect it:

file welcome.txt

You will see output like this:

welcome.txt: ASCII text

file has checked the contents and confirmed: "Yes, this is ASCII text."

Commands like cat, less, and editors work best when you already know whether a file is text. The file command helps you make that decision safely.

This means it is safe to read. You can use commands like cat:

cat welcome.txt

Check Binary File

System programs (like ls, mkdir, or cat) are not text files. They are binary files—written in a language only the computer processor understands.

Let's look at the ls command itself. It lives in the /bin folder.

Ask file to identify it:

file /bin/ls

The output might look scary, but it is very informative:

/bin/ls: ELF 64-bit LSB pie executable, x86-64, ...

Don't panic. Here is the translation:

  • ELF: Standard Linux program format (like .exe on Windows).
  • executable: A program you can run.

ELF stands for Executable and Linkable Format, which is the standard binary format used by many Linux systems. You do not need to remember the full name, but it is useful to recognize ELF as "this is a Linux executable."

The key takeaway is that file told you this is an executable. If you tried to cat /bin/ls, your screen would fill with weird symbols. Knowing a file is a binary warns you: "Do not read this, run it."

Identify Unknown Files

Now for the detective work. We have two suspicious files in your project folder: mystery_file and fake_image.jpg.

Case 1: The Nameless File

mystery_file has no extension. Is it text? A program?

Let's test it:

file mystery_file

Result:

mystery_file: PNG image data, ...

Aha! It is a PNG image. Even though it has no name tag, Linux identified the internal signature of a PNG picture.

Case 2: The Liar

Now look at fake_image.jpg. The label says "JPEG Image". But is it?

file fake_image.jpg

Result:

fake_image.jpg: ASCII text

It is a lie! It is just a text file.

Because we know it is text, we can safely read it:

cat fake_image.jpg

This specific skill—identifying files by content rather than name—is what separates pro Linux users from beginners.

It is also one reason Linux feels less extension-driven than some desktop operating systems. Extensions are helpful conventions, but the system is often capable of learning more by inspecting the file itself.

Summary

You have learned to look past the label and see the true nature of files.

  • file filename: The command to uncover the truth.
  • Extensions lie: .jpg doesn't always mean image.
  • No extension? No problem: Linux doesn't need extensions to know what a file is.

You are now equipped to handle any unknown file you encounter in the wild!