How to use the command od (with examples)
The od
command is a command-line utility that is used to display the contents of a file in various formats, such as octal, decimal, or hexadecimal. It can also display the byte offsets and printable representation for each line. It is a powerful tool that can come in handy when working with binary files or when you need to inspect the raw data in a file.
Use case 1: Display file using default settings
Code:
od path/to/file
Motivation: This use case is helpful when you want to quickly inspect the contents of a file and see them in octal format. The default settings of od
display the file with 8 bytes per line, byte offsets in octal, and duplicate lines replaced with *
.
Explanation: The od
command is followed by the path to the file you want to display. By default, od
uses octal format and displays 8 bytes per line. The byte offsets are shown in octal, and duplicate lines are replaced with *
.
Example output:
0000000 357 273 277 326 232 277 227 301
0000010 231 270 263 232 303 240 001 000
0000020 000 000 000 000 000 000 000 000
*
0000040 000 \n
0000041
Use case 2: Display file in verbose mode
Code:
od -v path/to/file
Motivation: By using the -v
(or --verbose
) option, you can display the file in a more verbose mode. In this mode, duplicate lines are not replaced with *
, which can be useful when you want to see every line in the file.
Explanation: The -v
option enables verbose mode, which means that duplicate lines are not replaced with *
.
Example output:
0000000 357 273 277 326 232 277 227 301
0000010 231 270 263 232 303 240 001 000
0000020 000 000 000 000 000 000 000 000
0000030 000 000 000 000 000 000 000 000
0000040 000 \n
0000041
Use case 3: Display file in hexadecimal format (2-byte units), with byte offsets in decimal format
Code:
od --format=x --address-radix=d -v path/to/file
Motivation: This use case is helpful when you want to display the file in hexadecimal format, with 2-byte units. Additionally, by using the --address-radix=d
option, the byte offsets are displayed in decimal format instead of octal.
Explanation: The --format=x
option specifies the format as hexadecimal. The --address-radix=d
option sets the address radix to decimal. The -v
option enables verbose mode, and the path to the file is specified as usual.
Example output:
0000000 1fbb1af2 e8bb1e31 f9bae823 100020a
0000010 ee13c223 1d9b3a4 10000000 10000000
0000020 10000000 10000000 10000000 10000000
0000030 10000000 10000000 10000000 10000000
0000040 10000000 \n
Use case 4: Display file in hexadecimal format (1-byte units), and 4 bytes per line
Code:
od --format=x1 --width=4 -v path/to/file
Motivation: Sometimes it can be useful to display the file in hexadecimal format with 1-byte units. The --width=4
option allows you to specify the number of bytes per line to be displayed.
Explanation: The --format=x1
option specifies the format as hexadecimal with 1-byte units. The --width=4
option sets the width to 4 bytes per line. The -v
option enables verbose mode, and the path to the file is specified as usual.
Example output:
0000000 1f bb 1a f2
0000004 e8 bb 1e 31
0000010 f9 ba e8 23
0000014 10 00 20 a0
0000020 ee 13 c2 23
0000024 1d 9b 3a 40
0000030 10 00 00 99
0000034 00 01 00 00
0000040
Use case 5: Display file in hexadecimal format along with its character representation, and do not print byte offsets
Code:
od --format=xz --address-radix=n -v path/to/file
Motivation: This use case is helpful when you want to see the file in hexadecimal format along with its corresponding character representation. Here, the --format=xz
option combines the hexadecimal format with the character representation.
Explanation: The --format=xz
option specifies the format as hexadecimal with character representation. The --address-radix=n
option disables the printing of byte offsets. The -v
option enables verbose mode, and the path to the file is specified as usual.
Example output:
1f bb 1a f2 e8 bb 1e 31 f9 ba e8 23
10 00 20 a0 ee 13 c2 23 1d 9b 3a 40
10 00 00 99 00 01 00 00
Use case 6: Read only 100 bytes of a file starting from the 500th byte
Code:
od --read-bytes 100 --skip-bytes=500 -v path/to/file
Motivation: This use case is helpful when you want to read only a specific range of bytes from a file. Here, we read 100 bytes starting from the 500th byte.
Explanation: The --read-bytes
option specifies the number of bytes to read, which in this case is 100. The --skip-bytes
option specifies the number of bytes to skip, which in this case is 500. The -v
option enables verbose mode, and the path to the file is specified as usual.
Example output:
00007d0 c3 b8 bd 00 bd 01 bd 02 bd bc bd 03 bd
00007e0 bd 04 bd 05 bd 06 bd 07 bd 08 bd 09 bd
00007f0 0a bd 0b bd 0c bd 0d bd 0e bd 0f bd 10
0000800 bd 11 bd 12 bd 13 bd 14 bd 15 bd 16 bd
0000810 17 bd 18 bd 19 bd 1a bd 1b bd 1c bd 1d
Conclusion:
The od
command is a versatile tool for displaying file contents in different formats. By understanding the various use cases and options available, you can effectively use od
to inspect binary files and understand the raw data they contain. Whether you need to view the file in octal, decimal, or hexadecimal format, or if you want to see the byte offsets and printable representation for each line, the od
command has you covered.