How to Create a Hex Dump Using the xxd Command in Linux

When you use the standard cat command to view a compiled binary file or an image, your Linux terminal will output a chaotic stream of unreadable symbols, question marks, and beeping noises. This is because the terminal is attempting to interpret raw binary code as standard ASCII text. If you are reverse-engineering a program, debugging a corrupted network packet, or simply need to see the raw underlying data of a file, you must use a hexadecimal dumper. The standard tool for this in Linux is the xxd command.

How to Create a Hexadecimal Dump

The xxd utility takes any file and translates its raw binary data into a highly structured, human-readable hexadecimal format.

To view the raw hex dump of a small image file, run:

xxd logo.png

Because the output will likely be thousands of lines long, it is highly recommended to pipe the output into the less command so you can scroll through it.

xxd logo.png | less

The standard output of xxd is divided into three distinct columns:

  1. The Offset: The far-left column (e.g., 00000000:) shows the byte offset, telling you exactly where you are located within the file.
  2. The Hex Data: The middle columns display the actual raw file data translated into pairs of hexadecimal numbers (e.g., 8950 4e47 0d0a 1a0a).
  3. The ASCII Translation: The far-right column attempts to translate those hex pairs back into readable text characters. If a byte represents an unprintable character, it is displayed as a simple dot (.). This column is incredibly useful for finding hidden text strings or metadata inside compiled programs.

How to Limit the Output

If you only need to inspect the file header (the very beginning of the file) to determine its file type, you can use the -l (length) flag to tell xxd to stop after a certain number of bytes.

xxd -l 32 logo.png

This command will only dump the first 32 bytes of the file and then immediately stop, saving you from scrolling through megabytes of data.

How to Reverse a Hex Dump Back into a Binary

The most powerful feature of xxd is that it works in reverse. If you save a hex dump to a text file, edit the hex values using a standard text editor (like nano), you can then use xxd to compile that text file back into a raw, functional binary. This is a common technique for patching software or modifying save files.

First, create the dump and save it to a text file:

xxd original_game.save > editable_dump.txt

After you edit editable_dump.txt, use the -r (reverse) flag to compile it back into binary form:

xxd -r editable_dump.txt > hacked_game.save

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.