Linux hexdump Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux hexdump command, a powerful tool for viewing and manipulating binary data. The hexdump command allows you to display the contents of a file or any other binary data in a human-readable hexadecimal format. We will start by introducing the basic usage of the hexdump command, and then dive deeper into customizing the output to suit your needs. This lab is designed to provide practical examples and help you gain a better understanding of working with binary data in the Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") subgraph Lab Skills linux/cat -.-> lab-422719{{"`Linux hexdump Command with Practical Examples`"}} linux/dd -.-> lab-422719{{"`Linux hexdump Command with Practical Examples`"}} end

Introduction to the hexdump Command

In this step, we will explore the hexdump command, a powerful tool in Linux for viewing and manipulating binary data. The hexdump command allows you to display the contents of a file or any other binary data in a human-readable hexadecimal format.

Let's start by running the basic hexdump command on a file:

hexdump ~/project/example.txt

Example output:

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

The output shows the hexadecimal representation of the file's contents, with the corresponding ASCII characters displayed on the right side.

The hexdump command provides several options to customize the output format. For example, you can use the -C (canonical) option to display the output in a more readable format:

hexdump -C ~/project/example.txt

Example output:

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

In this format, the hexadecimal values are displayed in groups of 2 bytes, and the corresponding ASCII characters are shown on the right side.

You can also use the hexdump command to view the contents of a device or any other binary data source. For example, to view the first 10 bytes of the /dev/urandom device (a source of random data), you can run:

hexdump -n 10 /dev/urandom

Example output:

a5 e9 f6 c0 f3 b1 8e 7a 4a 3d

In the next step, we will explore more advanced usage of the hexdump command and learn how to customize the output further.

Viewing Binary Data with hexdump

In this step, we will learn how to use the hexdump command to view and analyze binary data in more detail.

First, let's create a binary file to work with:

dd if=/dev/urandom of=~/project/binary_file.bin bs=1024 count=1

This command generates a 1KB binary file filled with random data.

Now, let's use hexdump to view the contents of the binary file:

hexdump -C ~/project/binary_file.bin

Example output:

00000000  b5 7f 04 3c 91 82 f7 06  dc 2b 8e 8e 5e 2e 6e 1b  |...<.....+..^.n.|
00000010  7a 9a 8e 4e 3a 5e 8e 2d  9b 3e 7d 0a 0d 5d 2e 8f  |z..N:^.-.:}..]..|
00000020  6d 17 a5 8a 1d 6f 9f 3d  44 f2 8e 3f 9a 8f 3a 5b  |m....o.=D..?..:[|
00000030  e1 73 2f 7e 5f 8f 04 a3  1e 4a 9e 8c 8f 9a 8e 4e  |.s/~_....J.....N|

The -C option displays the output in a canonical format, showing the hexadecimal values in groups of 2 bytes and the corresponding ASCII characters on the right side.

You can also use the hexdump command to view specific parts of the binary file. For example, to view only the first 16 bytes, you can use the -n (number of bytes) option:

hexdump -C -n 16 ~/project/binary_file.bin

Example output:

00000000  b5 7f 04 3c 91 82 f7 06  dc 2b 8e 8e 5e 2e 6e 1b  |...<.....+..^.n.|

Additionally, you can use the hexdump command to view the file in different formats, such as the canonical format (-C), the canonical display for single-byte character data (-c), or the 32-bit integer format (-x).

In the next step, we will explore how to customize the hexdump output further to suit your specific needs.

Customizing the hexdump Output

In this final step, we will explore how to customize the output of the hexdump command to suit your specific needs.

The hexdump command provides several options to control the format of the output. Let's start by using the -v (verbose) option to display additional information:

hexdump -v -C ~/project/binary_file.bin

Example output:

00000000  b5 7f 04 3c 91 82 f7 06  dc 2b 8e 8e 5e 2e 6e 1b  |...<.....+..^.n.|
00000010  7a 9a 8e 4e 3a 5e 8e 2d  9b 3e 7d 0a 0d 5d 2e 8f  |z..N:^.-.:}..]..|
00000020  6d 17 a5 8a 1d 6f 9f 3d  44 f2 8e 3f 9a 8f 3a 5b  |m....o.=D..?..:[|
00000030  e1 73 2f 7e 5f 8f 04 a3  1e 4a 9e 8c 8f 9a 8e 4e  |.s/~_....J.....N|

The -v option displays the file offset (the position of the data in the file) in the leftmost column.

You can also use the -x option to display the output in 32-bit hexadecimal format:

hexdump -x ~/project/binary_file.bin

Example output:

00000000  3c7f04b5  06f78291  8e2b0cdc  1b6e2e5e
00000010  8e3a9a7a  0a7d3e9b  8f2e5d0d  8a17a56d
00000020  3d9f6f1d  3f8ef244  5b3a8f9a  8c9e4a1e
00000030  8f04a37f  4e8e9a8f  73e1

In this format, the data is displayed in 32-bit hexadecimal values, which can be useful for analyzing binary data at a lower level.

Finally, let's try the -d option to display the output in 16-bit decimal format:

hexdump -d ~/project/binary_file.bin

Example output:

00000000  48879  32403  62353  56076  56462  24670  31259  14623
00000016  31610  15677  13010  21869  8207  37243  17694  14350
00000032  15995  17215  17787  17498  28509  17742

The -d option displays the data in 16-bit decimal values, which can be helpful for interpreting the binary data as numeric values.

By using these customization options, you can tailor the hexdump output to your specific needs and gain a deeper understanding of the binary data you're working with.

Summary

In this lab, we explored the Linux hexdump command, a powerful tool for viewing and manipulating binary data. We learned how to use the basic hexdump command to display the contents of a file in a human-readable hexadecimal format, and how to customize the output using various options such as the -C (canonical) option. We also discovered how to use hexdump to view the contents of devices and other binary data sources. Finally, we practiced viewing and analyzing binary data in more detail by creating a binary file and using hexdump to inspect its contents.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like