Linux xxd Command: Create and Reverse Hex Dumps

The xxd command is a standard command-line utility in the Linux operating system used to create hexadecimal representations of binary data, commonly known as hex dumps, and to perform the exact reverse operation. This guide covers how Linux leverages xxd to read files, format raw bytes into human-readable hex and ASCII formats, and reconstruct binary files from existing hex dumps for debugging, analysis, and data patching.

Creating Hex Dumps with xxd

In Linux, xxd generates a hex dump by taking input from a file or standard input and translating every byte into its corresponding two-digit hexadecimal value. By default, running the command against a target file displays three columns: the byte offset in hexadecimal, the hex representations of the data grouped in two-byte pairs, and the printable ASCII equivalents (with non-printable characters represented by periods).

To create a basic hex dump and output it to the terminal, use:

xxd input.bin

To redirect the generated hex dump into a text file for inspection or editing, supply an output filename:

xxd input.bin dump.hex

Linux users can customize this output using various flags:

Reversing Hex Dumps to Binary

The reverse operation, often called "reverting" or "unpacking," uses the -r flag. In this mode, xxd parses standard hex dump formatting—ignoring byte offsets and trailing ASCII characters—and writes the reconstructed raw binary data directly to an output file.

To convert an edited or existing hex dump back into a binary file:

xxd -r dump.hex output.bin

When working with continuous hex strings generated via the plain format (-p), the -p flag must be combined with the reverse flag:

xxd -r -p plain.hex output.bin

Patching and Offset Control

The reverse functionality also allows targeted patching. When an offset is specified in the hex file, xxd -r can write data to specific positions within an existing file without rewriting the entire payload:

Through this bidirectional translation, Linux utilizes xxd as an essential utility for inspecting compiled code, analyzing corrupted files, modifying payloads, and automating binary file generation within shell scripts.