Need a hex dump for a text file? Looking for a command line utility that can do this? Well, you are in luck, as there exists a command dubbed xxd that can do this for you. In this tutorial, we will discuss xxd using some easy to understand examples. But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.

Linux xxd command

The xxd command in Linux lets you create a hexdump or even do the reverse. Following is its syntax:

xxd [OPTIONS] [file]

And here's how the tool's man page explains it:

xxd creates a hex dump of a given file or standard input. It can also convert a hex dump back to its original binary form. Likeuuencode(1) and uudecode(1) it allows the transmission of binary data in a `mail-safe' ASCII representation, but has the advantageof decoding to standard output. Moreover, it can be used to perform binary file patching.

Following are some Q&A-styled examples that should give you a better idea on how xxd works.

Q1. How to use xxd command?

It's simple. For example, suppose you have a text file 'test.txt' with contents as shown below:

Q3. How to limit xxd output to a particular length?

In the previous section, we discussed how to make xxd start converting from a particular point. But there's also a way to limitize its conversion to a particular point. This can be done using the -l command line option.

For example, to make sure xxd creates dump for only the first three lines of test.txt, use it in the following way:

Q5. How to make xxd produce binary dump?

Yes, this is also possible. The -b command line options makes xxd produce dump in binary digits. Here's what the man page says about this tool:

-b | -bitsSwitch to bits (binary digits) dump, rather than hexdump. This option writes octets as eight digits "1"s and "0"s insteadof a normal hexadecimal dump. Each line is preceded by a line number in hexadecimal and followed by an ascii (or ebcdic)representation. The command line switches -r, -p, -i do not work with this mode.

Here's an example:

Conclusion

So you can see, the xxd command is useful if you deal in hex and binary file contents. Here, in this tutorial, we've just discussed some basic options xxd offers. Once you're done practicing these, head to the tool's man page to learn more about it.

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.