Linux Commands

How to Use hexdump Command in Linux?

Hexdump is a powerful tool in Linux systems that are mostly used by developers and app debuggers. It can convert input files and data into a pleasant and readable format.

Here’s a real-life example where hexdump may be helpful. If you’re working with binary data, this will be very difficult to understand. For ease, you can quickly turn the binary data into hexadecimal or decimal.

In this guide, check out how to use hexdump command in Linux.

Hexdump in Linux

Hexdump is a simple yet useful program that’s written in C language. That’s why professional C programmers may find its usage easy. However, even if you’re not experienced in C programming, you can still use hexdump for your purpose.

Hexdump comes pre-installed in any Linux distro. In this guide, I’ll be using Ubuntu for demonstration purposes.

Hexdump usage

For demonstrating the usage of hexdump, I’ve created a sample text file dummy.txt.

$ cat dummy.txt

We’ll be passing this file to hexdump to convert its content into various formats of output.

One-byte octal display

The following hexdump command will print the input data in hexadecimal format. In the output, each line contains 16 space-separated bytes of input data, each having 3 columns and zero-filled, in octal.

$ hexdump -b <input_file_content>

One-byte character display

The following hexdump command will display the input data in hexadecimal format. In the output, each line contains 16 space-separated characters of input data, each having 3 columns and space-filled.

$ hexdump -c <input_file_content>

Canonical hex + ASCII display

The following hexdump command will display the input data in hexadecimal. In the output, each line contains 16 space-separated hexadecimal bytes, each having 2 columns. The following content will be the same bytes in %_p format enclosed in “|” characters.

$ hexdump -C <input_file_content>

Two-byte decimal display

The following hexdump command will display the input data in hexadecimal format. In the output, each line contains 8 space-separated 2 bytes units of input data, each having 5 columns and zero-filled, in unsigned decimal.

$ hexdump -d <input_file_content>

Two-byte octal display

The following hexdump command will print the input data in hexadecimal format. In the output, each line contains 8 space-separated 2 bytes of input data, each with 6 columns and zero-filled, in octal.

$ hexdump -o <input_file_content>

Two-byte hexadecimal display

The following hexdump command will print the input data in hexadecimal format. In the output, each line contains 8 space-separated 2 bytes of input data, each with 4 columns and zero-filled, in hexadecimal.

$ hexdump -x <input_file_content>

Display all input

When using hexdump, it’ll replace the content of repeated lines with a single asterisk. If you want to force hexdump to output all the contents, then use the “-v” flag.

$ cat dummy.txt

$ hexdump -b dummy.txt

$ hexdump -v -b <input_file_content>

Limit amount of bytes

Hexdump supports the option of deciding a specific number of bytes from a file to hexdump. To specify the amount, use the flag “-s” followed by the byte amount.

$ hexdump -s 2 -c <input_file_content>

Final thought

The examples demonstrated here are the ways you’ll mostly use hexdump. For complete information on hexdump, the man page is one of the best resources.

$ man hexdump

Hexdump is a powerful tool if you know when and how to use it. However, tools aren’t always the answer when it comes to debugging. Check out how to debug a bash script. If you’re into NodeJS, then check out how to use the NodeJS debugger.

Enjoy!

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.