← Full reference

xxd and od

View a file's bytes in hex, octal and character form; rebuild a file from a hex dump

xxd and od show a file byte by byte — this reveals what normal output hides: \r from Windows, a BOM at the start, null bytes, an extra newline at the end, the encoding. xxd can also do the reverse: build a binary file from a hex string. od (octal dump) is available on any system as part of coreutils. The sandbox also has hexdump -C — its output is similar to xxd.

Syntax

xxd [options] [file [output_file]]
xxd -r [-p] [file [output_file]]     # reverse: from dump to bytes
od [options] [file…]

Without a file both commands read standard input.

Reading xxd output

00000000: 4865 6c6c 6f2c 2057 6f72 6c64 210a 0001  Hello, World!...
└─offset─┘ └──── 16 bytes in hex, 2 per group ────┘ └─ same bytes as text
  • Offset — the number of the line's first byte, in hex: 00000010 = 16.
  • Hex column — each byte as two digits: 48 = H, 0a = \n, 0d = \r, 00 = null byte.
  • Text column — printable ASCII characters as is, everything else as a dot.

xxd options

What and how much to show

OptionWhat it does
-l Nshow only N bytes
-s Nstart at byte N; -s -N — N bytes before the end of the file
-c Nbytes per line (16 by default; 30 for -p, 12 for -i)
-g Nbytes per group (2 by default): -g 1 — one at a time, as in hexdump -C
-areplace all-zero lines with an asterisk *
-o Nadd N to the displayed offsets
-doffsets in decimal

Numbers in -l, -s, -c, -o can be written in hex: -s 0x10.

Output format

OptionWhat it does
-p"plain" hex without offsets and text — handy for scripts and copying; -p -c 0 — everything on one line
-uuppercase hex: 4C instead of 4c
-bbinary: 01001000 instead of 48
-elittle-endian: bytes within a group in reverse order (default group of 4)
-ioutput as a C array (unsigned char name[] = {0x48, …}); -n name — the array name, -C — in uppercase
-R alwayscolorize the output (auto — only in a terminal, never — no)

Reverse conversion

OptionWhat it does
-rfrom a normal xxd dump back to bytes; the text column is ignored
-r -pfrom "plain" hex (48656c6c6f); spaces and newlines in it are allowed
-r into an existing fileif you give an output file, it is not truncated: bytes are written at the offsets from the dump — this is how individual bytes are "patched"

od options

Offsets and range

OptionWhat it does
-A d / -A x / -A o / -A noffsets in decimal / hex / octal (the default!) / no offsets
-j Nskip the first N bytes (0x10, 1K are allowed)
-N Nread at most N bytes
-w Nbytes per line (16 by default)
-vdon't collapse identical lines into *

Format: -t TYPE[SIZE]

TypeWhat it shows
-t x1hex per byte — the clearest option; x2, x4 — per 2/4 bytes (byte order as on the CPU, usually little-endian)
-t u1 / -t d1unsigned / signed decimal numbers per byte
-t o1octal bytes
-t ccharacters; special ones as \n, \t, \0, the rest as an octal code (377)
-t acharacter names: nl, ht, nul, sp
-t f4 / -t f8floating-point numbers
-t x1zadd a >…< text column on the right, like xxd
--endian=bigread multi-byte numbers as big-endian

Old short forms: -c = -t c, -b = -t o1, -x = -t x2, -d = -t u2. They can be combined: od -bc gives two lines for every 16 bytes.

Examples

printf 'Hello, World!\n\0\1\377' > data.bin

# the classic dump
xxd data.bin

# first 5 bytes / 5 bytes from byte 7 / the last 3 bytes
xxd -l 5 data.bin
xxd -s 7 -l 5 data.bin
xxd -s -3 data.bin

# are there \r\n and trailing spaces
printf 'line\r\n' | xxd

# BOM at the start of a UTF-8 file: ef bb bf
printf '\357\273\277text' | xxd -l 3

# "plain" hex and back
echo -n 'hi' | xxd -p              # 6869
echo '6869' | xxd -r -p            # hi

# fix one byte in a copy of a file: dump, edit, rebuild
xxd data.bin > dump.txt
sed -i '1s/^00000000: 48/00000000: 4a/' dump.txt
xxd -r dump.txt > patched.bin

# write a byte at offset 2 directly into an existing file
cp data.bin copy.bin
echo '00000002: 58' | xxd -r - copy.bin

# od: characters, hex per byte without offsets, decimal codes
od -c data.bin
od -A n -t x1 data.bin
od -A d -t u1 -N 4 data.bin

# hex with a text column and decimal offsets
od -A d -t x1z data.bin

# 4 random bytes as one hex string
od -A n -t x1 -N 4 /dev/urandom | tr -d ' \n'; echo

# the same as xxd -g1, via hexdump
hexdump -C data.bin

Pitfalls

  • xxd -l doesn't understand suffixes. xxd -l 1k shows one byte: the k is silently dropped. Write the full number (-l 1024) or hex (-l 0x400). With od -N 1K suffixes work.
  • od offsets are octal by default. 0000010 at the start of a line is byte 8, not 10. Add -A d or -A x.
  • od -x and -t x2 swap bytes. On a little-endian CPU, Hi shows up as 6948, not 4869. For "bytes as in the file" use -t x1 (or xxd, whose groups read left to right).
  • xxd -r without -p expects the format with a colon and an offset. Plain hex without -p won't be rebuilt: echo 6869 | xxd -r silently outputs nothing with code 0.
  • xxd -r into an existing file doesn't truncate it. That's handy for patches, but if you need a file made exactly from the dump, write to a new file or via >.
  • Multibyte characters. The letter é in UTF-8 is two bytes c3 a9, so in the xxd text column it's two dots, and od -c shows the octal codes 303 251.
  • od and xxd collapse identical lines differently: od replaces repeats with * by default, xxd — only with -a. If you count dump lines, add od -v.

See also

cat and tac · diff, cmp and patch · base64 · iconv · checksums · dd