Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
xxd [OPTION]... [FILE [OUTFILE]]
hexdump [OPTION]... FILE...

Try it

Read-only: typical xxd output for the bytes Hello, Linux! followed by a newline:

CONSOLE
$ printf 'Hello, Linux!\n' | xxd
00000000: 4865 6c6c 6f2c 204c 696e 7578 210a       Hello, Linux!.

Read-only: the same data with hexdump -C:

CONSOLE
$ printf 'Hello, Linux!\n' | hexdump -C
00000000  48 65 6c 6c 6f 2c 20 4c  69 6e 75 78 21 0a        |Hello, Linux!.|
0000000e
Bash The same dump with od, which is installed

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

Terminal
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? xxd ships with Vim, so it is present wherever Vim is; hexdump is in bsdextrautils or util-linux. Minimal containers often have neither; od is always there.
  • Endianness. hexdump without -C prints 16-bit words in the machine's byte order, so bytes look swapped on little-endian CPUs. xxd and hexdump -C always show bytes in file order.
  • Patching a file with xxd: dump to text, edit a hex digit, then xxd -r writes the binary back. Keep a backup; one wrong column breaks the file.
  • Hex to bytes for scripts: echo 48656c6c6f | xxd -r -p prints Hello. Without xxd, loop over pairs with printf "\\x$h".
  • Large files: limit output with -l/-n and -s, or pipe to less.
  • Repeated rows are shown as * by hexdump; xxd prints them all (use -a to collapse them).
  • Not for text. For readable strings inside binaries, use strings.