Linux xxd Command Tutorial for Beginners (with Examples)

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 22.04 LTS machine.

What is the Linux xxd command used for?

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.  Like
uuencode(1) and uudecode(1) it allows the transmission of binary data in a `mail-safe' ASCII representation, but has the  advantage
of 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 of 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:

No.|Country|Yes/No
01|India|Y
02|US|Y
03|Australia|Y

04|China|N
05|Russia|Y
06|Japan|Y

07|Singapore|Y
08|South Korea|N
09|Finland|Y
10|Ireland|Y

Here's how you can use xxd to convert the file contents into hex:

xxd test.txt

And here's the output:

00000000: 4e6f 2e7c 436f 756e 7472 797c 5965 732f  No.|Country|Yes/
00000010: 4e6f 0a30 317c 496e 6469 617c 590a 3032  No.01|India|Y.02
00000020: 7c55 537c 590a 3033 7c41 7573 7472 616c  |US|Y.03|Austral
00000030: 6961 7c59 0a0a 3034 7c43 6869 6e61 7c4e  ia|Y..04|China|N
00000040: 0a30 357c 5275 7373 6961 7c59 0a30 367c  .05|Russia|Y.06|
00000050: 4a61 7061 6e7c 590a 0a30 377c 5369 6e67  Japan|Y..07|Sing
00000060: 7061 6f72 657c 590a 3038 7c53 6f75 7468  paore|Y.08|South
00000070: 204b 6f72 6561 7c4e 0a30 397c 4669 6e61   Korea|N.09|Fina
00000080: 6c61 6e64 7c59 0a31 307c 4972 656c 616e  land|Y.10|Irelan
00000090: 647c 590a                                d|Y.

XXD Command

Q2. How to skip a few lines with xxd?

Suppose you don't want hex dump of the complete file. Instead, you want the tool to start converting from a specific line. Then this can be achieved using the -s command.

For example, if you want xxd to produce hex dump from line 3 onwards, then here's how you tell xxd to do this:

xxd -s 0x30 test.txt

Here's the output produced:

00000030: 6961 7c59 0a0a 3034 7c43 6869 6e61 7c4e  ia|Y..04|China|N
00000040: 0a30 357c 5275 7373 6961 7c59 0a30 367c  .05|Russia|Y.06|
00000050: 4a61 7061 6e7c 590a 0a30 377c 5369 6e67  Japan|Y..07|Sing
00000060: 7061 6f72 657c 590a 3038 7c53 6f75 7468  paore|Y.08|South
00000070: 204b 6f72 6561 7c4e 0a30 397c 4669 6e61   Korea|N.09|Fina
00000080: 6c61 6e64 7c59 0a31 307c 4972 656c 616e  land|Y.10|Irelan
00000090: 647c 590a                                d|Y.

So you can see the hex dump is produced from line 3 onwards in this case.

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 limit its conversion to a particular point. This can be done using the -l command-line option.

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

xxd -l 0x30 test.txt

Here's the output it produced:

00000000: 4e6f 2e7c 436f 756e 7472 797c 5965 732f  No.|Country|Yes/
00000010: 4e6f 0a30 317c 496e 6469 617c 590a 3032  No.01|India|Y.02
00000020: 7c55 537c 590a 3033 7c41 7573 7472 616c  |US|Y.03|Austral

Q4. How to set column length?

If you want xxd to produce fewer or more columns in the output, use the -c option and specify the number of columns there. Here's an example command using this option:

xxd -c 5 test.txt

And here's the output:

00000000: 4e6f 2e7c 43  No.|C
00000005: 6f75 6e74 72  ountr
0000000a: 797c 5965 73  y|Yes
0000000f: 2f4e 6f0a 30  /No.0
00000014: 317c 496e 64  1|Ind
00000019: 6961 7c59 0a  ia|Y.
0000001e: 3032 7c55 53  02|US
00000023: 7c59 0a30 33  |Y.03
00000028: 7c41 7573 74  |Aust
0000002d: 7261 6c69 61  ralia
00000032: 7c59 0a0a 30  |Y..0
00000037: 347c 4368 69  4|Chi
0000003c: 6e61 7c4e 0a  na|N.
00000041: 3035 7c52 75  05|Ru
00000046: 7373 6961 7c  ssia|
0000004b: 590a 3036 7c  Y.06|
00000050: 4a61 7061 6e  Japan
00000055: 7c59 0a0a 30  |Y..0
0000005a: 377c 5369 6e  7|Sin
0000005f: 6770 616f 72  gpaor
00000064: 657c 590a 30  e|Y.0
00000069: 387c 536f 75  8|Sou
0000006e: 7468 204b 6f  th Ko
00000073: 7265 617c 4e  rea|N
00000078: 0a30 397c 46  .09|F
0000007d: 696e 616c 61  inala
00000082: 6e64 7c59 0a  nd|Y.
00000087: 3130 7c49 72  10|Ir
0000008c: 656c 616e 64  eland
00000091: 7c59 0a       |Y.

So you can see only 5 columns were produced in output.

Q5. How to make xxd produce binary dump?

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

-b | -bits
Switch to bits (binary digits) dump, rather than hexdump.  This option writes octets as eight digits "1"s and  "0"s  instead
of  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.

Command:

xxd -b test.txt

Here's an example:

xxd -b result

Conclusion

As 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.

Share this page:

2 Comment(s)