Viewing files
od
Dump a file's bytes as octal, hex, decimal or characters, to see exactly what it contains.
od [OPTION...] [FILE...]
od (octal dump) prints the raw contents of a file in a readable notation. Its default output
is 2-byte octal words, which is rarely what anyone wants today; the useful forms are od -c
(each byte as a character or an escape like \r or \n) and od -t x1 (each byte in hex).
od is part of coreutils and POSIX, so it is on every Unix system, which makes it the safe
choice in scripts. For interactive hex viewing, xxd and hexdump -C print a friendlier
side-by-side hex and text layout. Use od when you need its -t type control: decoding
bytes as 4-byte integers, floats, or with a chosen byte order.
Each output line starts with the byte offset, in octal unless you change it with -A.
Examples
See every byte as a character
od -c data/windows.txt
0000000 l i n e o n e \r \n l i n e t 0000020 w o \r \n 0000024
-c shows printable bytes as themselves and control bytes as C escapes, so the \r \n
pairs reveal CRLF line endings. The offsets on the left are octal: 0000020 is byte 16.
Hex bytes without offsets
od -An -t x1 todo.txt | head -n 2
62 75 79 20 6d 69 6c 6b 0a 66 69 78 20 6c 6f 67 69 6e 20 62 75 67 0a 77 72 69 74 65 20 74 65 73
-t x1 prints one byte per hex number and -An drops the offset column. 62 75 79 is
buy, 20 a space and 0a the newline.
Hex next to printable text
od -A d -t x1z -N 32 photos/beach.png
0000000 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 >.PNG........IHDR< 0000016 00 00 02 80 00 00 01 e0 08 02 00 00 00 ba b3 4b >...............K< 0000032
The z suffix adds the printable characters between > and < at the end of each line,
-A d makes the offsets decimal and -N 32 stops after 32 bytes. The .PNG signature and
the IHDR chunk name stand out.
Read a few bytes from a given offset
od -A d -t x1 -j 16 -N 8 photos/beach.png
0000016 00 00 02 80 00 00 01 e0 0000024
-j 16 skips the first 16 bytes and -N 8 reads 8. In a PNG those are the image width and
height: 00 00 02 80 and 00 00 01 e0.
Decode bytes as big-endian integers
od --endian=big -An -t u4 -j 16 -N 8 photos/beach.png
640 480
-t u4 reads 4-byte unsigned integers. PNG stores numbers big-endian, so --endian=big
decodes them correctly: the image is 640 by 480.
The machine's byte order by default
printf '\001\000\000\000' | od -An -t d4
1
Without --endian, multi-byte values use the CPU's byte order. On this little-endian x86
machine, the bytes 01 00 00 00 are the number 1; a big-endian reading would give 16777216.
Byte values in decimal
printf '\377\376A' | od -An -t u1
255 254 65
-t u1 prints each byte as an unsigned decimal number: 255, 254 and 65 (the letter A).
Change how many bytes per line
od -An -c -w8 todo.txt | head -n 3
b u y m i l k \n f i x l o g i n b u g \n w
-w8 puts 8 bytes on each line instead of 16, which is easier to read for short records.
The historical default format
od README.md | head -n 2
0000000 020043 062544 067555 005012 020101 066563 066141 020154 0000020 060563 070155 062554 070040 067562 062552 072143 072440
Plain od prints 2-byte words in octal, in machine byte order, which is hard to relate to
the text. This is why nearly everyone types od -c or od -t x1.
Common pitfalls
Offsets are octal by default:
0000020means byte 16, not 20. Add-A dfor decimal or-A xfor hex offsets.Multi-byte types (
-t x2,-t d4, plainod) use the CPU's byte order, so the same file dumps differently on different machines. Use-t x1to see bytes as they are stored, or--endian.Repeated identical lines are collapsed to a single
*. Add-vwhen you need every line, for example to diff two dumps.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
--endian,-wand thezsuffix on-tare GNU additions. For scripts that must also run on BSD/macOS, stick to the POSIX options-A,-j,-N,-t,-cand-v.
Options worth knowing
A selection, described in our own words. For the full list, see od(1) or od --help.
| Option | What it does |
|---|---|
-c | Show bytes as printable characters or C-style escapes; same as -t c. |
-t, --format TYPE | Output type: c, a, x1, x2, o1, u1, d4, f8 and so on; add z to append printable text. |
-A, --address-radix RADIX | Offset format: d (decimal), o (octal, default), x (hex) or n (none). |
-j, --skip-bytes BYTES | Skip this many bytes of input first. |
-N, --read-bytes BYTES | Dump at most this many bytes. |
-v, --output-duplicates | Print every line; do not replace repeats with *. |
-w, --width[=BYTES] | Bytes per output line (32 if given without a value). |
--endian ORDER | Byte order for multi-byte types: big or little. |
-x | Same as -t x2: 2-byte hex words. |
-b | Same as -t o1: 1-byte octal. |
-a | Same as -t a: named characters, ignoring the high bit. |
Related
xxdMake a hex dump of a file, or turn a hex dump back into binary with -r.hexdumpDisplay file contents in hex, octal, decimal or ASCII, with a canonical hex-plus-text layout via -C.catConcatenate files and print them to standard output; also the quickest way to see a short file.fileGuess what kind of data a file holds by looking at its contents, not its name.