Mastering the Linux file Command: Identify Real File Types

Overview

Unlike Windows environments that rely heavily on file extensions to determine file formats, Linux never blindly trusts a filename suffix. Extensions can be freely modified, forged, or omitted altogether. This practical walkthrough demonstrates how systems administrators and developers leverage the versatile file utility to unmask authentic underlying file architectures—accurately classifying binaries, plaintext sources, compressed payloads, and standard MIME formats.

Fundamental Operations and Exposing Spoofed File Extensions

Start by initializing a clean sandbox directory to stage the test files, switching into it immediately via mkdir -p ~/file_lab && cd ~/file_lab.

Setting up the testing workspace

Populate a simple ASCII-encoded text file for verification tests using echo 'Hello Linux World' > sample.txt.

Generating initial text asset

Under the hood, the file utility inspects magic numbers, structural headers, and byte patterns stored within the initial segments of a file rather than reading its trailing name string.
Invoking this utility against our newly generated asset confirms that the payload consists of pure ASCII text.
⚙️ [Essential CLI Flags]

-b : Strips file path prefixes from stdout, producing a clean, type-only brief response.
-i : Outputs RFC-compliant MIME types (such as text/plain; charset=us-ascii) rather than colloquial descriptions.
-z : Inspects inside compressed archives to analyze the encapsulated target data directly.
-L : Dereferences symbolic links to report on the underlying destination file instead of the pointer itself.
-f : Takes an input file containing a list of target paths to perform batch classification.

Executing the basic file inspection command

To demonstrate how Linux handles spoofed filenames, duplicate the text document while appending a graphic image extension with cp sample.txt fake_image.jpg.

Cloning the text document with a fake JPEG extension

Now inspect fake_image.jpg using the file utility.
Even though the filename exhibits a .jpg suffix, Linux evaluates the inner byte signatures and correctly reveals its true nature as an ASCII text document, completely bypassing the deceptive naming.

Detecting the actual text structure of a spoofed image

Querying MIME Types for Scripts and Web Services (-i)

Return to the practice directory to proceed with web integration tests using cd ~/file_lab.

Navigating to testing workspace

Executing file -i sample.txt yields standardized media identifiers and character set encodings.
Switching human-readable output to machine-readable strings like text/plain; charset=us-ascii is indispensable when constructing automated shell workflows, backend upload validators, or dynamic HTTP response headers.

Extracting MIME classification and charset parameters

Next, build a structured JSON payload to examine how structured data serializations are detected: echo '{"name": "ubuntu", "version": 24.04}' > data.json.

Writing sample JSON document to disk

Audit the freshly generated file format using file -i data.json.
Because JSON represents structured plaintext, the engine accurately categorizes the resource under standard plaintext MIME classifications and matching character set profiles.

Confirming MIME metadata for JSON records

Re-enter the staging directory to set up archive and link evaluations using cd ~/file_lab.

Returning to test environment

Package and compress the existing test files into an archive bundle using tar -czf archive.tar.gz sample.txt data.json.

Bundling sample files into a gzipped tarball

Running standard analysis via file archive.tar.gz examines the outer wrapper.
The utility identifies the perimeter format as gzip compressed data.

Checking outer compressed archive signature

To look past the compression envelope without manually uncompressing files to disk, execute file -z archive.tar.gz.
The -z flag instructs the engine to decompress the stream in memory, exposing the internal POSIX tar archive structure directly.

Peeking through compression layers into tar contents

To explore link behavior, establish a symbolic link referencing the initial text asset using ln -s sample.txt link_sample.

Establishing a symbolic link

Checking the pointer with file link_sample evaluates the link entity itself.
By default, the command reports that the path is a symbolic link pointing to sample.txt.

Inspecting link metadata directly

Add the -L switch by running file -L link_sample to follow the pointer.
This dereferences the link, causing the command to traverse the filesystem pointer and report the true format of the target file: ASCII text.

Dereferencing symbolic link to identify the target asset

Inspecting Native Binaries and Special Device Nodes

Run file /bin/bash to deconstruct the internal profile of the default system shell executable.
The resulting diagnostics present an exhaustive profile: an ELF 64-bit LSB pie executable targeted for the x86-64 architecture, complete with dynamic linkage (dynamically linked) attributes.

Analyzing the Bash binary header details

Next, evaluate the kernel discard sink with file /dev/null.
Rather than identifying a storage file, the utility flags it as a character special device designed for data sink operations.

Auditing the dev null character special device

Similarly, inspect the system zero-byte stream generator via file /dev/zero.
Like /dev/null, the operating system registers this virtual node as a kernel-managed character special interface.

Examining dev zero device node

These fundamental techniques showcase how the file utility provides deep architectural visibility across filesystems, archives, and system nodes. Integrating these flags into your day-to-day Linux workflow ensures accurate format detection and reliable data validation regardless of deceptive naming conventions.