od command in Linux with example
The od (octal dump) command in Linux is a versatile tool used to display file contents in various formats, with the default being octal. This command is particularly useful for debugging scripts, examining binary files, or visualizing non-human-readable data like executable code. It allows users to view data in multiple formats, including octal, hexadecimal, decimal, and ASCII, providing a detailed look into the file's contents. The 'od' command is essential for developers and system administrators when troubleshooting unwanted changes, hidden characters, or encoding issues in files.
Syntax:
od [OPTION]... [FILE]...
This command reads the contents of one or more files and displays the output in the specified format. If multiple files are provided, 'od' concatenates them in the listed order before processing.
Common Options of od command
Here are the most commonly used options with the 'od' command, along with examples and explanations to help you understand their usage:
1. -b Option: Display in Octal Format
It displays the contents of input in octal format.
Syntax:
$ od -b input.txt
Example:
input :
100
101
102
103
104
105
Output:

Explanation: The first column represents the byte offset in the file, followed by the octal representation of the file contents.
2. -c Option: Display in Character Format
It displays the contents of input in character format.
Syntax:
$ od -c input.txt
Example:
input :
100
101
102
103
104
105
Output:

3. -An Option: Display Without Offsets
It displays the contents of input in character format but with no offset information.
Syntax:
$ od -An -c input.txt
Example:
input :
100
101
102
103
104
105
Output:

4. -A Option: Display with Custom Offsets
It displays the contents of input in different format by concatenation some special character with -A.
For example:
- -Ax for Hexadecimal format(we concatenate x with -A)
- -Ao for Hexadecimal format(we concatenate o with -A)
- -Ad for Hexadecimal format(we concatenate d with -A)
Syntax:
$ od -Ax input.txt $ od -Ao input.txt $ od -Ad input.txt
Example:
input :
100
101
102
103
104
105
Output:

5. - Option: Accept Input from the Command Line
Accept input from command line.
Syntax:
$ od -c -
Example:

Here, we see that first the input was given and then after pressing the ctrl+d the od command output was displayed.
6. Display hidden character in a file
Consider the following content of file :
Geek ^Mforgeeks
If a file containing the above string is printed using the cat command, following output is seen :
$ cat file
$ forgeekseek
So, in order to remove it we use,
$ od -c file
Output:
0000000 G e e k f o r \r g e e k s \n
0000020
7. -j Option: Skip Bytes
It displays the output after skipping some bytes.
Syntax:
$ od -j4 -c input.txt
Example:
input :
100
101
102
103
104
105
Output:

Explanation: Here,initial 4 bytes were skipped from output.
8. -N Option: Display Limited Bytes
It display limited bytes in output using -N option.
Syntax:
$ od -N4 -c input.txt
Example:
input :
100
101
102
103
104
105
Output:

Explanation: Here,initial 4 bytes were displayed from output.It is the opposite of -j option.
9. -w Option :
It is used to customize the output width.
Syntax:
$ $ od -w1 -c -Ad input.txt
Example:
input :
100
101
102
103
104
105
Output:

Explanation: So we see that output width was reduced to 1
10. -v Option: Display All Data Including Duplicates
It is used to output duplicate values. As can be observed in the output above, a * was printed. This is done to suppress the output of lines that are same or duplicates. But through -v option these lines can also be printed.
Syntax:
$ $ od -w1 -v -c -Ad input.txt
Example:
input :
100
101
102
103
104
105
Output:

11. -i Option: Display as Decimal Integers
Shows the file content as decimal integers, which is helpful for numerical data analysis.
Syntax:
$ $ od -i input.txt
Example:
input :
100
101
102
103
104
105
Output:

12. -o Option: Display as Octal 2-Byte Units
Converts and displays the data in octal, formatted as 2-byte units.
Syntax:
$ $ od -o input.txt
Example:
input :
100
101
102
103
104
105
Output:

13. -x Option: Display as Hexadecimal 2-Byte Units
Shows the file contents as hexadecimal 2-byte units, ideal for viewing binary data.
Syntax:
$ $ od -x input.txt
Example:
input :
100
101
102
103
104
105
Output:

14. -t Option: Select Specific Output Format
It select output format and display it. Traditional format specifications may be intermixed; they accumulate: '-a' same as '-t a', select named characters, ignoring high-order bit '-b' same as '-t o1', select octal byte '-c' same as '-t c', select printable characters or backslash escapes '-d' same as '-t u2', select unsigned decimal 2-byte units '-f' same as '-t fF', select floats '-i' same as '-t dI', select decimal ints '-l' same as '-t dL', select decimal longs '-o' same as '-t o2', select octal 2-byte units '-s' same as '-t d2', select decimal 2-byte units '-x' same as '-t x2', select hexadecimal 2-byte units.
Syntax:
$ $ od -ta input.txt
Example:
input :
100
101
102
103
104
105
Output:

15. --help Option: Display Help Information
Use this option to display the help information for the 'od' command, including details about all available options.
Syntax:
$ $ od --help
Output:

16. --version Option: Display Version Information
Shows the version of the 'od' command installed on your system.
Syntax:
$ $ od --version
Output:

Conclusion
The 'od' command in Linux is a powerful utility for converting and displaying file contents in various formats. From octal and hexadecimal to ASCII and more, 'od' provides a detailed view of data that is not easily readable. Its wide range of options and flexibility make it a go-to tool for debugging, data analysis, and understanding complex file structures.