Master the Linux file Command: Identify Real File Types

Overview

In Unix-like environments, relying on file extensions is an unreliable way to determine what a file actually contains—extensions can be stripped, altered, or forged entirely. Rather than wading through dense manual entries, this hands-on guide walks through practical terminal exercises to reveal how the file utility uses magic numbers and internal signatures to pinpoint genuine file formats.

1. Inspecting True File Formats and Unmasking Fake Extensions

Set up a dedicated sandbox directory for this walkthrough and switch into it by running mkdir -p ~/file_demo && cd ~/file_demo.

Initialize workspace directory

Generate a standard plain text document to serve as a baseline test subject using echo 'Hello Linux System' > text_sample.txt.

Generate sample plain text file

Assemble an executable shell script and assign execution rights with echo '#!/bin/bash\necho "Running script"' > script_sample.sh && chmod +x script_sample.sh.

Create executable shell script

Simulate an extension spoofing scenario by copying the executable script into an image filename via cp script_sample.sh fake_image.jpg.

Duplicate script with an image extension

The file utility ignores arbitrary filename endings, probing header signatures (magic numbers) and data structures instead.
Running the command against the initial sample confirms it as pure ASCII text.
⚙️ [Essential Command Switches]

-b : Strips filenames from the standard output, delivering only the detected type string.
-i : Outputs standard MIME format strings (such as text/plain or image/jpeg) rather than human-oriented summaries.
-z : Inspects the interior payload of compressed archives without requiring manual extraction first.
-L : Dereferences symbolic links to report the type of the underlying target rather than the link itself.
-f : Ingests a text file of paths to analyze large batches in a single operation.
-s : Reads block or character device nodes directly to identify underlying filesystem structures.

Examine plain text file

Executing file script_sample.sh demonstrates that the tool distinguishes general text from a specialized POSIX shell script (Bourne-Again shell script).

Verify shell script type

Testing the spoofed artifact with file fake_image.jpg exposes the deception immediately: despite the .jpg suffix, the utility correctly identifies the payload as a shell script.

Detect disguised executable behind image extension

2. Inspecting MIME Types and Running Batch Scans (-i, -b Flags)

Ensure your shell prompt is located in the exercise folder by executing cd ~/file_demo.

Switch to workspace directory

Verify that current packages for both file and gzip are present on the host with apt-get update -qq && apt-get install -y file gzip.

Install file and gzip tools

Create an archived test target to evaluate compression handling by issuing gzip -c text_sample.txt > archive_sample.gz.

Generate gzip archive sample

Querying MIME metadata via file -i text_sample.txt demonstrates error handling behavior when the target cannot be located in the current path.

Attempt MIME check on missing file

Inspect the archive using file -i archive_sample.gz to check its MIME signature.
Because this sample contains an empty payload, the utility identifies it with the inode/x-empty MIME type along with binary charset metadata.

Examine MIME attributes of archive

Request brief format output without prepending filenames by entering file -b text_sample.txt.
When the referenced file cannot be accessed, only the raw error string is presented.

Execute brief file type check

Employ shell wildcards to classify all directory entries simultaneously through file *.
The scanner differentiates between empty archives and nested subdirectories in one sweep.

Batch scan entire working directory

Navigate back into the primary testing directory using cd ~/file_demo.

Return to demo folder

Construct a symbolic reference targeting our text file with ln -s text_sample.txt link_to_text.

Create symbolic link

By default, executing file link_to_text examines the pointer itself, acknowledging the item as a symbolic link and displaying its target reference.

Inspect symbolic link metadata

Passing the dereference flag via file -L link_to_text instructs the tool to traverse the pointer and classify the destination target as ASCII text.

Dereference link with -L flag

Running file archive_sample.gz will fail with an error prompt if the archive file is absent from the current working directory.

Attempt inspection on non-existent archive

Attempting to evaluate uncompressed payload contents directly with file -z archive_sample.gz also halts when the file path cannot be resolved.

Test transparent archive inspection

4. Probing Device Files and Processing Target Lists (-s, -f Flags)

Reposition the terminal session to the project directory with cd ~/file_demo.

Re-enter demo directory

Compile an inventory of local directory contents into a structured manifest using ls -1 > targets.txt.

Generate itemized target list

Process the entire batch sequentially from the saved list by executing file -f targets.txt.
The utility processes scripts, symbolic pointers, and standard documents in one automated pass.

Process list of targets in batch

Analyze special system nodes using the -s flag with file -s /dev/null.
Because the null device discards all incoming data and returns immediate EOF, the tool reports it as an empty entity.

Examine null device attributes

Query the zero-byte data generator device via file -s /dev/zero.
Reading the continuous stream of null bytes leads the utility to classify the underlying resource as generic binary data.

Classify zero-byte character device

This walkthrough highlighted the core capabilities of the Linux file utility, demonstrating how to look past deceptive naming schemes and verify data integrity using internal signatures.
Leverage these techniques whenever you encounter ambiguous binaries, missing extensions, or suspect payloads in your administrative workflows.