hexdump

2025-01-17

Basic Usage: Getting Started with hexdump

The simplest use of hexdump is to display the hexadecimal representation of a file. Let’s create a simple text file named sample.txt with the content “Hello, world!”:

echo "Hello, world!" > sample.txt

Now, let’s use hexdump to view its hexadecimal content:

hexdump sample.txt

This will output something similar to:

0000000 4865 6c6c 6f2c 2077 6f72 6c64 210a
0000008

Each line shows an offset (0000000), followed by hexadecimal bytes (4865 6c6c etc.), representing the ASCII characters. The 0a at the end signifies a newline character.

Refining the Output: Customizing hexdump’s Behavior

hexdump offers a range of options for tailoring the output to your needs. Let’s look at some key options:

-C (Canonical Format): A More Readable Output

The -C option provides a more human-readable canonical format, including ASCII representation alongside the hexadecimal data:

hexdump -C sample.txt

This will produce an output like this:

00000000  48 65 6c 6c 6f 2c 20 77  |Hello, w|
00000008  6f 72 6c 64 21 0a        |orld!.|
0000000a

The right-hand column displays the ASCII interpretation of the hexadecimal bytes, making it easier to correlate the binary data with its textual representation.

-n (Number of Bytes): Limiting the Output

To display only a specific number of bytes, use the -n option:

hexdump -n 8 sample.txt

This will only show the first 8 bytes of sample.txt.

-s (Offset): Starting at a Specific Offset

The -s option allows you to begin the dump from a specific byte offset:

hexdump -s 4 -n 4 sample.txt

This starts at the 5th byte (offset 4) and displays the next 4 bytes.

-b (Bytes): One Byte per Line

The -b option displays one byte per line, useful for very detailed analysis:

hexdump -b sample.txt

Advanced Usage: Working with Different Formats and Addressing Data Structures

hexdump isn’t limited to text files. You can examine any file type, potentially revealing its internal structure. For instance, you can inspect executable files or image files to understand their binary composition.

Experimenting with different combinations of the options above, along with exploring other less frequently used options in the man hexdump page, can reveal the full power of this versatile command-line tool. Understanding the underlying binary representation of data is critical in many computing tasks, and hexdump provides an indispensable means to achieve this.

For more complex scenarios involving specific data structures, understanding how your data is laid out in memory is essential. You’ll often need to use hexdump in conjunction with other tools for detailed analysis.