The Wayback Machine - https://web.archive.org/web/20250313181018/https://www.geeksforgeeks.org/hexdump-command-in-linux-with-examples/
Open In App

hexdump command in Linux with examples

Last Updated : 01 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The ‘hexdump’ command in Linux is a versatile utility used to display file content or data from standard input in a human-readable format. It is invaluable for programmers and system administrators for debugging binary data, analyzing file structures, and verifying data integrity.

Here we will get a detailed look at ‘hexdump’, covering its syntax, options, usage examples, and its role in various debugging scenarios.

What is the ‘hexdump’ Command?

‘hexdump’ transforms binary files into a readable format that includes hexadecimal and ASCII representations, making it easier to inspect the file’s raw data.

Syntax:

hd [OPTIONS...] [FILES...]

Key Options Available with ‘hexdump’ Command

1. ‘-b’: One-byte octal display. Display the input offset in hexadecimal, followed by sixteen space-separated, three column, zero-filled, bytes of input data, in octal, per line.

Syntax:

hd -b input.txt

The first column of the output represents the input offset in file.

2. ‘-c’: One-byte character display. Display the input offset in hexadecimal, followed by sixteen space-separated, three column, space-filled, characters of input data per line.

Syntax:

hd -c input.txt

3. ‘-C’: Canonical hex+ASCII display. Display the input offset in hexadecimal, followed by sixteen space-separated, two column, hexadecimal bytes, followed by the same sixteen bytes in ‘%_p’ format enclosed in “|” characters.

Syntax:

hexdump -C input.txt

4. ‘-d’: Two-byte decimal display. Display the input offset in hexadecimal, followed by eight space-separated, five column, zero-filled, two-byte units of input data, in unsigned decimal, per line.

Syntax:

hd -d input.txt

5. ‘-n’ length: Where length is an integer. Interprets only ‘length’ bytes of output.

Syntax:

hd -n length input.txt

6. ‘-o’: Two-byte octal display. Display the input offset in hexadecimal, followed by eight space-separated, six column, zero-filled, two byte quantities of input data, in octal, per line.

Syntax:

hd -o input.txt

7. ‘-s’ offset : Skip ‘offset’ bytes from the beginning of the input. By default, offset is interpreted as a decimal number. With a leading 0x or 0X, offset is interpreted as a hexadecimal number, otherwise, with a leading 0, offset is interpreted as an octal number. Appending the character b, k, or m to offset causes it to be interpreted as a multiple of 512, 1024, or 1048576, respectively.

Syntax:

hd -s offset input.txt

As you can see, in the output, first 6 characters, i.e. ‘Hello ‘ are skipped.

8. ‘-v’: Cause hexdump to display all input data. Without the -v option, any number of groups of output lines, which would be identical to the immediately preceding group of output lines (except for the input offsets), are replaced with a line comprised of a single asterisk. Syntax:

hd -v input.txt

We will see the use of this option as we display the output using ‘-c' flag.

As you can observe, when we use ‘hd' for the first time, without -v, when similar output appears, it prints out an asterisk (*). But when we pass a ‘-v' flag, we get all the output lines.

9. ‘-x’: Two-byte hexadecimal display. Display the input offset in hexadecimal, followed by eight, space separated, four column, zero-filled, two-byte quantities of input data, in hexadecimal, per line.

Syntax:

hd -x input.txt

hexdump command in Linux with examples – FAQs

What is the hexdump command used for in Linux?

The hexdump command in Linux is a utility that displays the contents of a file in hexadecimal format. It’s commonly used by programmers and system administrators to inspect binary files, such as executables or data files, in a detailed and byte-by-byte manner. This tool is particularly valuable for debugging issues with file formats, examining the differences between binary files, and other tasks where seeing the exact content of a file is necessary.

How to display file contents in hexadecimal using hexdump?

To display the contents of a file in hexadecimal, you can simply use the hexdump command followed by the filename. For example, to view the contents of a file named example.bin in hexadecimal, you would use:

hexdump example.bin

This command will output the file data in hexadecimal format, showing both the offset and the data bytes at that offset.

Can hexdump show both ASCII and hexadecimal output?

Yes, hexdump can display both ASCII and hexadecimal output simultaneously. To do this, you can use the -C option, which formats the output in a canonical hexadecimal + ASCII display. Here is how you can use it:

hexdump -C example.bin

This command displays each line of the file with the hexadecimal bytes on the left and the corresponding ASCII characters (if printable) on the right.

How to customize the format of hexdump output?

hexdump offers the ability to customize its output format through the use of format strings. This is done with the -e option, which allows you to specify a format string that controls the display of data. Here’s an example:

hexdump -e '4/4 "%08X " "\n"' example.bin

In this command:

  • 4/4 specifies that hexdump should process four units of four bytes each per line.
  • "%08X " specifies that each unit should be displayed as a zero-padded, eight-character hexadecimal value.
  • "\n" adds a newline after each set of four units.

This format customization can be very powerful depending on the specific needs of the task at hand, such as aligning output to match the structure of data blocks in a file.

What are some common uses of hexdump in debugging?

hexdump is frequently used in various debugging scenarios:

  1. Binary Data Analysis: When working with binary data formats, hexdump can help visualize the data structure and content, facilitating the understanding of how data is stored and manipulated within the file.
  2. Corruption Detection: hexdump is useful for examining files that may be corrupted. By comparing hex dumps of good and bad files, you can sometimes pinpoint the location and nature of data corruption.
  3. Protocol Analysis: For developers working with custom network protocols or analyzing network packet dumps, hexdump can display the packet contents that are not easily readable otherwise.
  4. Reverse Engineering: It is often used in reverse engineering to understand the workings of a binary file when source code is not available.
  5. Software Development: hexdump can be used to verify that data written by a program is in the correct format, especially in systems programming and embedded software development.



Next Article

Similar Reads

three90RightbarBannerImg