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.

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

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

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

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.

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

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.

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.

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.

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

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

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.

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.

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

3. Peeking Inside Archives and Resolving Symlinks (-z, -L Flags)
Navigate back into the primary testing directory using cd ~/file_demo.

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

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

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.

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

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

4. Probing Device Files and Processing Target Lists (-s, -f Flags)
Reposition the terminal session to the project directory with cd ~/file_demo.

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

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.

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.

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.

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.