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
| Option | What it does |
|---|---|
-l N | show only N bytes |
-s N | start at byte N; -s -N — N bytes before the end of the file |
-c N | bytes per line (16 by default; 30 for -p, 12 for -i) |
-g N | bytes per group (2 by default): -g 1 — one at a time, as in hexdump -C |
-a | replace all-zero lines with an asterisk * |
-o N | add N to the displayed offsets |
-d | offsets in decimal |
Numbers in -l, -s, -c, -o can be written in hex: -s 0x10.
Output format
| Option | What it does |
|---|---|
-p | "plain" hex without offsets and text — handy for scripts and copying; -p -c 0 — everything on one line |
-u | uppercase hex: 4C instead of 4c |
-b | binary: 01001000 instead of 48 |
-e | little-endian: bytes within a group in reverse order (default group of 4) |
-i | output as a C array (unsigned char name[] = {0x48, …}); -n name — the array name, -C — in uppercase |
-R always | colorize the output (auto — only in a terminal, never — no) |
Reverse conversion
| Option | What it does |
|---|---|
-r | from a normal xxd dump back to bytes; the text column is ignored |
-r -p | from "plain" hex (48656c6c6f); spaces and newlines in it are allowed |
-r into an existing file | if 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
| Option | What it does |
|---|---|
-A d / -A x / -A o / -A n | offsets in decimal / hex / octal (the default!) / no offsets |
-j N | skip the first N bytes (0x10, 1K are allowed) |
-N N | read at most N bytes |
-w N | bytes per line (16 by default) |
-v | don't collapse identical lines into * |
Format: -t TYPE[SIZE]
| Type | What it shows |
|---|---|
-t x1 | hex per byte — the clearest option; x2, x4 — per 2/4 bytes (byte order as on the CPU, usually little-endian) |
-t u1 / -t d1 | unsigned / signed decimal numbers per byte |
-t o1 | octal bytes |
-t c | characters; special ones as \n, \t, \0, the rest as an octal code (377) |
-t a | character names: nl, ht, nul, sp |
-t f4 / -t f8 | floating-point numbers |
-t x1z | add a >…< text column on the right, like xxd |
--endian=big | read 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 -ldoesn't understand suffixes.xxd -l 1kshows one byte: thekis silently dropped. Write the full number (-l 1024) or hex (-l 0x400). Withod -N 1Ksuffixes work.odoffsets are octal by default.0000010at the start of a line is byte 8, not 10. Add-A dor-A x.od -xand-t x2swap bytes. On a little-endian CPU,Hishows up as6948, not4869. For "bytes as in the file" use-t x1(orxxd, whose groups read left to right).xxd -rwithout-pexpects the format with a colon and an offset. Plain hex without-pwon't be rebuilt:echo 6869 | xxd -rsilently outputs nothing with code 0.xxd -rinto 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 bytesc3 a9, so in thexxdtext column it's two dots, andod -cshows the octal codes303 251. odandxxdcollapse identical lines differently:odreplaces repeats with*by default,xxd— only with-a. If you count dump lines, addod -v.
See also
cat and tac · diff, cmp and patch · base64 · iconv · checksums · dd