Inspect a File's Likely Type

Inspect a File's Likely Type

Someone sends the teacher report.data, and she is not sure whether it is text, an image or a compressed file. The suffix gives a clue, but a filename can be misleading. The file utility inspects content using tests and known signatures to report a likely type.

This is a useful inspection step before choosing how to open unfamiliar data. It is not a complete validation or a promise that a file is safe. We will give a plain-text report a vague name to see why the bytes matter more than the extension alone.

What you’ll learn

  • Inspect file content with the file utility.
  • Explain why changing an extension does not convert or compress a file.

Content and filename are separate clues

  1. Name

    The filename does not claim a text extension.

    report.data
  2. Bytes

    The report still contains a plain-text mark line.

    Diya: 72
  3. Inspection

    file examines content and reports a likely type.

    text, despite .data
Treat a suffix as a clue and content inspection as another clue.

Inspect a text report with a .data name

Prepare the example

Use a fresh folder with the file utility installed. The first line writes an ordinary mark line to a file ending in .data. We deliberately choose that name so the later report must be based on inspection rather than a friendly .txt label.

Bash · Linux terminal

printf 'Diya: 72\n' > report.data
file report.data

Expected behavior; machine-dependent details

A description of text data; exact wording varies.

file report.data examines the saved input and prints a description. For this small example, expect a text-related result; exact wording can vary with the utility's version and the data. The report does not become a special binary format just because its suffix is .data.

The same reasoning applies when you rename a text report to .zip. That name change does not run a compressor or construct an archive. Use the appropriate conversion or compression tool when the contents need to change. Inspection helps you choose that tool, but a likely type still needs the application's own format checks when accuracy matters.

Inspect text, images and ambiguous filenames

Linux does not need filename extensions to determine what a file contains. The file command examines the data inside a file and reports the most likely file type.

What does the file command do?

The file command examines the actual contents of a file and tells you what type of data it contains — whether it’s plain text, a PNG image, a shell script, a binary executable, or something else entirely.

Inspect sample files with the file command

Let’s try it out. First, let’s list the files in our directory:

ls

Let’s say you have the following files:

notes.txt  image.png  script.sh  unknown_file

Now, let’s run the file command on each of them.

1. Check a text file:

file notes.txt
notes.txt: ASCII text

2. Check an image file:

file image.png
image.png: PNG image data, 800 x 600, 8-bit/color RGBA

3. Check a shell script:

file script.sh
script.sh: Bourne-Again shell script, ASCII text executable

4. Check a file with no extension:

file unknown_file
unknown_file: ELF 64-bit LSB executable, x86-64, version 1 (SYSV)...

Wow! Even without an extension, Linux was able to identify that the last file is a compiled program (an executable).

Useful ways to apply the file command

  • You can check multiple files at once:
    file * 
  • This command works great on files downloaded from the internet — always check them before opening!

Identify file contents with the file command

The file command is a quick and reliable way to determine what a file actually is, based on its contents, not just its name. It’s one of those tools every Linux user should know!

Try it, then check the worked answer

Your task: Does renaming report.txt to report.zip compress it?

Use a fresh folder with the file utility installed. The first line writes an ordinary mark line to a file ending in .data. We deliberately choose that name so the later report must be based on inspection rather than a friendly .txt label.

Bash · complete example for this question

printf 'Diya: 72\n' > report.data
file report.data

Expected behavior; machine-dependent details

A description of text data; exact wording varies.

Worked answer

No; a name change leaves its content unchanged.

Renaming report.txt to report.zip leaves its bytes unchanged. The extension may influence how an application tries to open it, but it does not create a ZIP archive. Use a real archive command and verify its contents if a ZIP is what you need.

Check your decision

Does renaming report.txt to report.zip compress it?
Choose one answer. You can try again.

Answer and explanation

No; a name change leaves its content unchanged.

Renaming report.txt to report.zip leaves its bytes unchanged. The extension may influence how an application tries to open it, but it does not create a ZIP archive. Use a real archive command and verify its contents if a ZIP is what you need.

The extension does not convert the contents

Treat a suffix as a clue and content inspection as another clue. Neither is a universal validation guarantee. Choose a viewer or conversion tool based on the actual data, and do not confuse a rename with a transformation of file contents.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Create a Symbolic Link →