Session and utilities

The xxd command

Dumps a file as hexadecimal with its text beside it. With -r it turns a dump back into binary.

Syntax

xxd [opciones] [archivo [salida]]Try it in the terminal →

Most useful options

-c <columnas>Bytes per line. The default is 16 and the maximum is 256.
-g <bytes>Bytes per group in the normal output. The default is 2; with -g 1 each byte is separate.
-l <longitud>Stops after that many input bytes.
-s [+][-]<desplazamiento>Starts at that offset in the file. With -r, the value is added to the addresses in the dump.
-rReverts: turns a hexadecimal dump back into binary.
-uWrites the hexadecimal letters in uppercase.
-bShows the bytes in binary instead of hexadecimal.

Copy-ready examples

$ echo hola | xxddump of a word with its newline.

Example output

00000000: 686f 6c61 0a                             hola.
$ echo hola | xxd -c 4four bytes per line.

Example output

00000000: 686f 6c61  hola
00000004: 0a         .
$ echo hola | xxd -g 1groups each byte on its own.

Example output

00000000: 68 6f 6c 61 0a                                   hola.
$ echo hola | xxd | xxd -rturns the dump back into text: the original input comes out.

Example output

hola
$ head -c 4 imagen.png | xxdlooks at the first bytes of a file. The PNG signature starts with 89 50 4e 47.

Example output

00000000: 8950 4e47                                .PNG

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
xxd: nofile: No such file or directoryThe file does not exist or the name is mistyped.List the folder with ls and copy the exact name.
xxd: invalid number of columns (max. 256).You asked for more than 256 columns with -c. xxd does not accept more.Use 256 columns at most, or keep the default of 16.

Typical mistakes

  • Non-printable bytes show as a dot in the text column: the newline from echo appears as a dot.
  • Without a second argument, xxd -r writes the binary to standard output. To create a file, pass the output name as the second argument.
  • A large file produces a huge dump: limit the output with -l or with head.

Differences from similar commands

  • od: dumps in octal with no text column by default. xxd uses hexadecimal, shows the text on the right and can revert the dump.
  • hexdump: with -C it shows hexadecimal and encloses the text in | bars. xxd shows it without bars, and -r reverts the dump.

Related commands

FAQ

How do I change the base of the number on the left?

xxd shows it in hexadecimal; with -d it switches to decimal. With -o you add a value to it, which helps when comparing with another dump.

How can I change the number of columns?

With -c. For example, -c 8 shows eight bytes per line. With -g you decide how they are grouped within the line.

How do I edit a binary by hand?

Dump the file with xxd file > dump.txt, change the bytes in hexadecimal and convert it back with xxd -r dump.txt newfile.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course