· 1 min read

xxd easy to use way record

# Dev Note
This article was auto-translated from Chinese. Some nuances may be lost in translation.

xxd is a Linux tool that allows you to view binary files.

Why Would You Want to Look at Binary Files?

  • To check if the compiled program matches your expectations
  • To inspect the content of image or video files
  • Pure curiosity

Using xxd

xxd filename

This command will output the file content like this:

00000000: 4500 6d00 7000 7400 7900 0000 0000 0000  E.m.p.t.y.......
00000010: 0000 0000 0000 0000 0000 0000 0000 0000  ................

The output can be divided into three sections: the first is the hex dump address, with each line dumping 16 characters (by default), the second is the file content (displayed in ASCII encoding), and the far right shows the raw values of the file. If there are any unprintable characters, they will be represented by a ..

xxd -r filename

This command can revert the binary file back to its original form.

Some Helpful Combinations

  1. xxd filename | vim -: View the binary file using vim
  2. Open vim filename and then within it type :%!xxd -r to edit the binary file using vim
  3. xxd filename > dump.hex: Save the output directly to a file, which can be used with diff for comparison