Linux xxd Command Guide: Generate Hex Dumps and Reverse Them

Overview

When inspecting the underlying byte layout of binary executables, firmware blobs, or obfuscated text files in Linux, the xxd utility serves as an indispensable tool in any engineer’s toolkit. This walkthrough covers foundational concepts of xxd, demonstrating how to generate structured hexadecimal representations, tailor column layouts and file offsets, export byte sequences directly into C code, and reconstruct raw files from hex streams. Follow along with these practical scenarios to sharpen your low-level diagnostics and file-investigation workflow.

Installing xxd and Generating Your First Hex Dump

To begin experimenting with hexadecimal inspection, ensure the xxd package is installed on your system.

Updating package index and installing xxd

Next, create a simple test file to inspect by running echo "Hello Linux xxd Command!" > sample.txt.

Generating sample text file

Invoking xxd translates arbitrary file contents or standard input streams into a formatted hex dump.
The default presentation organizes data into three distinct sections: memory offset markers on the left, raw hexadecimal values in the middle, and human-readable ASCII representations along the right flank.
⚙️ [Key Options]

-p : Emits a continuous stream of raw hexadecimal values without memory offsets or ASCII decodings.
-r : Operates in reverse mode, converting hex dump input back into its original binary or text representation.
-c : Configures the exact number of bytes displayed per line.
-l : Restricts processing to a specified byte length.
-s : Skips ahead to a designated starting offset before dumping data.
-b : Formats the output in binary bit patterns rather than base-16 hex pairs.
-u : Renders hexadecimal letters in uppercase.
-i : Formats raw bytes as a ready-to-use C language array definition.

Executing standard xxd dump on sample text

Controlling Output Range and Column Width (-l, -c, -s)

Passing the -l flag instructs xxd to parse only the first 16 bytes of the target file.
This bounded inspection proves especially handy whenever you need to identify magic numbers or inspect metadata headers across massive binaries without flooding your console buffer.

Displaying the first 16 bytes using -l

With the -c parameter, you can override the standard 16-byte row formatting and compress each line down to 8 bytes.
Narrowing down the column width ensures terminal sessions with restricted horizontal real estate remain clean, readable, and well-aligned.

Formatting dump output to 8 columns with -c

Combining -s with -l lets you jump directly to offset 6 and extract precisely 10 subsequent bytes.
Targeted seeks like this allow developers to isolate embedded payloads or pinpoint specific byte sequences tucked deep within arbitrary offsets.

Slicing a 10-byte block from offset 6

Plain Hex Output (-p) and Exporting to C Source Arrays (-i)

The -p flag yields plain hexadecimal notation, omitting metadata columns such as addresses and printable ASCII characters.
This uninterrupted representation integrates seamlessly into shell scripts and downstream data pipelines requiring raw hex strings.

Dumping continuous plain hex format

Applying -i serializes the input stream into a valid C-style array structure.
Because it automatically generates both the byte array and an accompanying length constant, this flag eliminates tedious manual data formatting when packaging assets directly into embedded firmware or static binaries.

Generating a C language byte array definition

Execute xxd -p sample.txt > plain_hex.txt && cat plain_hex.txt to save the raw stream into an external file and inspect its contents immediately.
Capturing raw hex data like this creates ready-to-ship payloads suitable for network transmission or reverse-conversion tests.

Saving and verifying plain hex dump output

Reconstructing Original Files from Hex Dumps (-r)

To explore reversing hex back to binary, initialize a sample data source by running echo "4c696e757820526f636b73210a" > hex_data.txt.

Writing test hex string into text file

Pairing -r with -p reads the continuous hex values stored in hex_data.txt and reconstructs the decoded stream into restored.txt.
This reversal capability lets you safely reconstruct corrupted binaries or rehydrate assets that were encoded into hexadecimal for transport.

Converting plain hex back to original text

Verify the fidelity of the restored file by printing its contents with cat restored.txt.
The serialized hexadecimal sequences successfully translate back into the original readable message (“Linux Rocks!”).

Viewing restored file contents

You can also export a complete standard dump (dump.hex), invert it back into a new file using -r, and validate the round-trip byte integrity using the cmp utility.
Because cmp exits silently without printing discrepancies, you can be certain that every single byte survived the round-trip transformation without corruption.

Performing round-trip verification with cmp

Mastering xxd offers a fast, versatile way to dissect binary files and reconstruct raw payloads across the Linux terminal.
Integrate these practical patterns into your daily debugging, reverse engineering, and low-level system administration tasks.