Similar to od, xxd produces a hexadecimal or binary dump of a file in several different formats. It can also do the reverse, converting from its hex dump format back into the original data. For example, here’s a hex dump of binary file /usr/bin/who:
$ xxd /usr/bin/who 0000000: 7f45 4c46 0101 0100 0000 ... 0000 .ELF............ 0000010: 0200 0300 0100 0000 a08c ... 0000 ............4... 0000020: 6824 0000 0000 0000 3400 ... 2800 h$......4. ...(. 0000030: 1900 1800 0600 0000 3400 ... 0408 ........4...4... ...
The left column indicates the file offset of the row, the next eight columns contain the data, and the final column displays the printable characters in the row, if any. By default, xxd outputs three columns: file offsets, the data in hex, and the data as text (printable characters only).
Syntax:
# xxd [parameter]
Common parameters
Some of the commonly used parameters:
Command Option | Description |
---|---|
-l N | Display only the first N bytes. (Default displays the entire file,) |
-s N | Skip the first N bytes of the file. |
-s -N | Begin N bytes from the end of the file. (There is also a +N syntax for more advanced skipping through standard input; see the manpage.) |
-c N | Display N bytes per row. (Default=16) |
-g N | Group each row of bytes into sequences of N bytes, separated by whitespace, like od -s. (Default=2) |
-b | Display the output in binary instead of hexadecimal. |
-u | Display the output in uppercase hexadecimal instead of lowercase. |
-p | Display the output as a plain hexdump, 60 contiguous bytes per line. |
-r | The reverse operation: convert from an xxd hex dump back into the original file format. Works with the default hexdump format and, if you add the -p option, the plain hexdump format. |
-i | Display the output as a C programming language data structure. When reading from a file, it produces an array of unsigned chars containing the data, and an unsigned int containing the array length. When reading from standard input, it produces only a comma-separated list of hex bytes. |
xxd Command Examples
1. Use the -a parameter to automatically skip blanks, starting from 0x200, input file:
# xxd -a -s +0x200 geek.txt
2. Use the -a, -c parameters, automatically skip blanks, display 12 bytes per line, starting from 0x200, input file:
# xxd -a -c 12 -s +0x200 geek.txt
3. Use the -a, -c, and -g parameters to automatically skip blanks. Each line displays 12 bytes, one byte per line, and 512 bytes of content. Starting from 0x200, input file:
# xxd -a -c 12 -g 1 -l 512 -s +0x200 geek.txt