Linux Command Reference ยท Viewing and Searching Files
Linux xxd and hexdump commands
Reference for xxd and hexdump on Linux: print hex dumps, reverse them back to binary, use plain hex output, and choose od when neither tool is installed.
xxd and hexdump both turn a file into a hex dump: the offset, the bytes in hexadecimal and a column of printable characters. They are the tools of choice for reading binary formats, checking encodings and patching files by hand. xxd can also reverse a dump back into binary, which makes it a small hex editor.
Neither tool is installed in our sandbox, so the first examples below are read-only, and the runnable ones use od, which produces the same information.
xxd [OPTION]... [FILE [OUTFILE]] hexdump [OPTION]... FILE...
Try it
Read-only: typical xxd output for the bytes Hello, Linux! followed by a newline:
$ printf 'Hello, Linux!\n' | xxd 00000000: 4865 6c6c 6f2c 204c 696e 7578 210a Hello, Linux!.
Read-only: the same data with hexdump -C:
$ printf 'Hello, Linux!\n' | hexdump -C 00000000 48 65 6c 6c 6f 2c 20 4c 69 6e 75 78 21 0a |Hello, Linux!.| 0000000e
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
xxd [-b] [-c COLS] [-g BYTES] [-l LEN] [-s OFFSET] [-p] [-r] [-i] [FILE] hexdump [-C] [-n LEN] [-s OFFSET] [-e FORMAT] [-v] FILE...
| Option | Tool | Description |
|---|---|---|
-C |
hexdump | Canonical layout: hex bytes plus an ASCII column. |
-n LEN |
hexdump | Dump only LEN bytes. |
-s OFFSET |
both | Start at this byte offset. |
-e FORMAT |
hexdump | Custom output format string. |
-v |
hexdump | Show repeated lines instead of *. |
-l LEN |
xxd | Stop after LEN bytes. |
-c COLS |
xxd | Bytes per line (default 16). |
-g BYTES |
xxd | Group size in bytes (default 2). |
-p |
xxd | Plain continuous hex, no offsets or text. |
-r |
xxd | Reverse: convert a hex dump (or -p hex) back to binary. |
-i |
xxd | Output a C array, useful for embedding a file in source code. |
-b |
xxd | Binary digits instead of hex. |
Returns: 0 on success; non-zero on read errors or invalid reverse input.
Usage notes
- Which one do I have?
xxdships with Vim, so it is present wherever Vim is;hexdumpis inbsdextrautilsorutil-linux. Minimal containers often have neither;odis always there. - Endianness.
hexdumpwithout-Cprints 16-bit words in the machine's byte order, so bytes look swapped on little-endian CPUs.xxdandhexdump -Calways show bytes in file order. - Patching a file with xxd: dump to text, edit a hex digit, then
xxd -rwrites the binary back. Keep a backup; one wrong column breaks the file. - Hex to bytes for scripts:
echo 48656c6c6f | xxd -r -pprintsHello. Without xxd, loop over pairs withprintf "\\x$h". - Large files: limit output with
-l/-nand-s, or pipe toless. - Repeated rows are shown as
*byhexdump;xxdprints them all (use-ato collapse them). - Not for text. For readable strings inside binaries, use
strings.
Related
od,strings,file,dd- Lessons: Hex and Binary Inspection