Intro To 'od' Command In Linux
2023-11-29 - By Robert Elder
I use the 'od' command to produce an octal dump of a binary file:
od some-data.data
0000000 126336 063702 154056 062120 136412 025460 165341 027573
0000020 045214 153767 004775 067575 116003 067304 070714 056442
0000040 164125 132220 071351 116062 111561 000151 143444 123100
0000060 164501 104064 117721 125511 026304 030473 055520 153605
0000100 014111 144375 131562 113227 047402 030756 176507 126265
0000120 031375 022145 037655 136560 003766 022232 127526 132504
0000140 007454 003110
0000144
Using 'od' Like 'xxd' Or 'hexdump'
I can also use it to produce a hexadecimal dump of a file:
od -v -A x -t x2z --endian=big some-data.dat
000000 deac c267 2ed8 5064 0abd 302b e1ea 7b2f >...g..Pd..0+..{/<
000010 8c4a f7d7 fd09 7d6f 039c c46e cc71 225d >.J....}o...n.q"]<
000020 55e8 90b4 e972 329c 7193 6900 24c7 40a6 >U....r2.q.i.$.@.<
000030 41e9 3488 d19f 49ab c42c 3b31 505b 85d7 >A.4...I..,;1P[..<
000040 4918 fdc8 72b3 9796 024f ee31 47fd b5ac >I...r....O.1G...<
000050 fd32 6524 ad3f 70bd f607 9a24 56af 44b5 >.2e$.?p....$V.D.<
000060 2c0f 4806 >,.H.<
000064
just like the 'xxd' command:
xxd some-data.dat
00000000: deac c267 2ed8 5064 0abd 302b e1ea 7b2f ...g..Pd..0+..{/
00000010: 8c4a f7d7 fd09 7d6f 039c c46e cc71 225d .J....}o...n.q"]
00000020: 55e8 90b4 e972 329c 7193 6900 24c7 40a6 U....r2.q.i.$.@.
00000030: 41e9 3488 d19f 49ab c42c 3b31 505b 85d7 A.4...I..,;1P[..
00000040: 4918 fdc8 72b3 9796 024f ee31 47fd b5ac I...r....O.1G...
00000050: fd32 6524 ad3f 70bd f607 9a24 56af 44b5 .2e$.?p....$V.D.
00000060: 2c0f 4806 ,.H.
or the 'hexdump' command:
hexdump -C some-data.dat
00000000 de ac c2 67 2e d8 50 64 0a bd 30 2b e1 ea 7b 2f |...g..Pd..0+..{/|
00000010 8c 4a f7 d7 fd 09 7d 6f 03 9c c4 6e cc 71 22 5d |.J....}o...n.q"]|
00000020 55 e8 90 b4 e9 72 32 9c 71 93 69 00 24 c7 40 a6 |U....r2.q.i.$.@.|
00000030 41 e9 34 88 d1 9f 49 ab c4 2c 3b 31 50 5b 85 d7 |A.4...I..,;1P[..|
00000040 49 18 fd c8 72 b3 97 96 02 4f ee 31 47 fd b5 ac |I...r....O.1G...|
00000050 fd 32 65 24 ad 3f 70 bd f6 07 9a 24 56 af 44 b5 |.2e$.?p....$V.D.|
00000060 2c 0f 48 06 |,.H.|
00000064
Using 'od' To Inspect A Bitmap File
Here, I have a simple bitmap image file that's just a completely black image:
ls -l my-image.bmp
-rw-rw-r-- 1 robert robert 6220922 Nov 7 13:14 my-image.bmp
This 'od' command outputs lines that start with the byte offset, which is then followed by the file data. Both the address and the file data are presented in octal notation:
od my-image.bmp
0000000 046502 166172 000136 000000 000000 000172 000000 000154
0000020 000000 003600 000000 002070 000000 000001 000030 000000
0000040 000000 166000 000136 027043 000000 027043 000000 000000
0000060 000000 000000 000000 043502 071522 000000 000000 000000
0000100 000000 000000 000000 000000 000000 000000 000000 000000
*
0000140 000000 000000 000000 000000 000000 000002 000000 000000
0000160 000000 000000 000000 000000 000000 000000 000000 000000
*
27566160 000000 000000 000000 000000 000000
27566172
I can use these flags to make the output more readable:
od -A x -t x2z --endian=big my-image.bmp
000000 424d 7aec 5e00 0000 0000 7a00 0000 6c00 >BMz.^.....z...l.<
000010 0000 8007 0000 3804 0000 0100 1800 0000 >......8.........<
000020 0000 00ec 5e00 232e 0000 232e 0000 0000 >....^.#...#.....<
000030 0000 0000 0000 4247 5273 0000 0000 0000 >......BGRs......<
000040 0000 0000 0000 0000 0000 0000 0000 0000 >................<
*
000060 0000 0000 0000 0000 0000 0200 0000 0000 >................<
000070 0000 0000 0000 0000 0000 0000 0000 0000 >................<
*
5eec70 0000 0000 0000 0000 0000 >..........<
5eec7a
The '-A x' flag formats the addresses in hexadecimal, and the '-t x2' flag formats the file data as two byte hexadecimal values. The 'z' on the end of 'x2z' enables the literal character output that you see to the right of the hexdump. The '--endian=big' flag swaps the byte order.
By default, repeated lines are represented as an asterisk, which can be disabled with the '-v' flag:
od -v -A x -t x2z --endian=big my-image.bmp
000000 424d 7aec 5e00 0000 0000 7a00 0000 6c00 >BMz.^.....z...l.<
000010 0000 8007 0000 3804 0000 0100 1800 0000 >......8.........<
000020 0000 00ec 5e00 232e 0000 232e 0000 0000 >....^.#...#.....<
000030 0000 0000 0000 4247 5273 0000 0000 0000 >......BGRs......<
000040 0000 0000 0000 0000 0000 0000 0000 0000 >................<
000050 0000 0000 0000 0000 0000 0000 0000 0000 >................<
000060 0000 0000 0000 0000 0000 0200 0000 0000 >................<
000070 0000 0000 0000 0000 0000 0000 0000 0000 >................<
000080 0000 0000 0000 0000 0000 0000 0000 0000 >................<
000090 0000 0000 0000 0000 0000 0000 0000 0000 >................<
0000a0 0000 0000 0000 0000 0000 0000 0000 0000 >................<
0000b0 0000 0000 0000 0000 0000 0000 0000 0000 >................<
0000c0 0000 0000 0000 0000 0000 0000 0000 0000 >................<
0000d0 0000 0000 0000 0000 0000 0000 0000 0000 >................<
0000e0 0000 0000 0000 0000 0000 0000 0000 0000 >................<
0000f0 0000 0000 0000 0000 0000 0000 0000 0000 >................<
000100 0000 0000 0000 0000 0000 0000 0000 0000 >................<
000110 0000 0000 0000 0000 0000 0000 0000 0000 >................<
000120 0000 0000 0000 0000 0000 0000 0000 0000 >................<
000130 0000 0000 0000 0000 0000 0000 0000 0000 >................<
... Many Lines Omitted...
5eec50 0000 0000 0000 0000 0000 0000 0000 0000 >................<
5eec60 0000 0000 0000 0000 0000 0000 0000 0000 >................<
5eec70 0000 0000 0000 0000 0000 >..........<
5eec7a
Using 'od' To Extract Integers, Characters & Floating Point Values
The 'od' command can extract parts of a file as integers, floating points or characters as described in the 'info' pages:
info od
...
‘-t TYPE’
‘--format=TYPE’
Select the format in which to output the file data. TYPE is a
string of one or more of the below type indicator characters. If
you include more than one type indicator character in a single TYPE
string, or use this option more than once, ‘od’ writes one copy of
each output line using each of the data types that you specified,
in the order that you specified.
Adding a trailing “z” to any type specification appends a display
of the single byte character representation of the printable
characters to the output line generated by the type specification.
‘a’
named character, ignoring high-order bit
‘c’
printable single byte character, C backslash escape or a 3
digit octal sequence
‘d’
signed decimal
‘f’
floating point (*note Floating point::)
‘o’
octal
‘u’
unsigned decimal
‘x’
hexadecimal
...
Using 'od' To Read Bitmap Image Header Fields
Here's a script called 'print-bitmap-header.sh' that uses 'od' to print all fields in the bitmap file's header:
#!/bin/bash
BITMAP_FILE=my-image.bmp
echo "Header Identifier (Should be 'BM'): $(od -A n -j 0x0 -N 2 -t c ${BITMAP_FILE})"
echo "Size Of BMP file in bytes: $(od -A n -j 0x2 -N 4 -t u4 ${BITMAP_FILE})"
echo "Reserved: $(od -A n -j 0x6 -N 2 -t u2 ${BITMAP_FILE})"
echo "Reserved: $(od -A n -j 0x8 -N 2 -t u2 ${BITMAP_FILE})"
echo "Offset Of Image Data: 0x$(od -A n -j 0xA -N 4 -t x4 ${BITMAP_FILE})"
echo "Size Of Bitmap Header In Bytes: $(od -A n -j 0xE -N 4 -t u4 ${BITMAP_FILE})"
echo "Bitmap Width In Pixels: $(od -A n -j 0x12 -N 2 -t d2 ${BITMAP_FILE})"
echo "Bitmap Height In Pixels: $(od -A n -j 0x16 -N 2 -t d2 ${BITMAP_FILE})"
echo "Number Of Color Planes: $(od -A n -j 0x1A -N 2 -t u2 ${BITMAP_FILE})"
echo "Bits Per Pixel: $(od -A n -j 0x1C -N 2 -t u2 ${BITMAP_FILE})"
echo "Compression Method Used: $(od -A n -j 0x1E -N 4 -t u4 ${BITMAP_FILE})"
echo "Raw Bitmap Data Size: $(od -A n -j 0x22 -N 4 -t u4 ${BITMAP_FILE})"
echo "Horizontal Resolution (pixels/meter): $(od -A n -j 0x26 -N 4 -t d4 ${BITMAP_FILE})"
echo "Vertical Resolution (pixels/meter): $(od -A n -j 0x2A -N 4 -t d4 ${BITMAP_FILE})"
echo "Num Colors: $(od -A n -j 0x2E -N 4 -t d4 ${BITMAP_FILE})"
echo "Num Important Colors: $(od -A n -j 0x32 -N 4 -t d4 ${BITMAP_FILE})"
If I run the above script on my bitmap image, I'll see the following output:
chmod u+x print-bitmap-header.sh
./print-bitmap-header.sh
Header Identifier (Should be 'BM'): B M
Size Of BMP file in bytes: 6220922
Reserved: 0
Reserved: 0
Offset Of Image Data: 0x 0000007a
Size Of Bitmap Header In Bytes: 108
Bitmap Width In Pixels: 1920
Bitmap Height In Pixels: 1080
Number Of Color Planes: 1
Bits Per Pixel: 24
Compression Method Used: 0
Raw Bitmap Data Size: 6220800
Horizontal Resolution (pixels/meter): 11811
Vertical Resolution (pixels/meter): 11811
Num Colors: 0
Num Important Colors: 0
The 'od' Command Is A POSIX Command
Unlike the xxd or hexdump commands, the 'od' command is included in the POSIX specification. This makes it much more likely to come pre-installed on many platforms.
And that's why the 'od' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|