TIL od command line tool

od is a useful little tool if you want to get a dump of your file in hexadecimal, or octal, or even carefully printing out all the printable characters and escape sequences.

Say you had a file named hello.txt that contained just

hello world

And if you did a od ./hello.txt by default it would print out the content of the file in octal representation. So you might get something like:

$ od hello.txt 
0000000 062550 066154 020157 067567 066162 005144
0000014

The first column represents the file offset in octal. So in the first line, we are starting from byte 0. And each subsequent number represents two bytes in octal format. And the second line represents the byte offset 12 (which is 14 in octal). I can’t read octal numbers for shit as it is, and this output is confusing for reasons beyond that.

So let me try to print it in hexadecimal instead. You can change the format with the -t flag. -t x1z tells od to print the contents of the file in hexadecimal, identifying each byte. The z in x1z tells od to also print the readable characters at the end of each line.

You can also tell od to use hexadecimal values for the file offset in the first column of each line by setting -A x.

Say the content of hello.txt is now

hello world how are you I am fine

Then putting our flags together:

$ od -A x -t x1z ./hello.txt
000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 20 68 6f 77 20  >hello world how <
000010 61 72 65 20 79 6f 75 20 49 20 61 6d 20 66 69 6e  >are you I am fin<
000020 65 0a                                            >e.<
000022

So as you can see, the first column represents the file offset, and each line covers 16 bytes. The first column of the first line 000000 indicates we are starting from the 0th byte in this line. The first column of the second line 000010 indicates we are starting from the 16th byte of the file (10 is hexadecimal for the decimal value 16).

Each column after the file offset value represents one byte. Taking the first line as our example, 68 is hexadecimal for 105 which is the ASCII value for ‘h’, 65 is hexadecimal for 101 which is the ASCII value for ‘e’, and so on.

There’s lots of other flags and options. You can represent multiple bytes per column instead of one byte per column, you can set the file offset to start and end on, you can represent the file offset or file content as decimals, whatever works for your particular use case.