The od command in Linux converts displays the contents of a file in different formats. Octal is the default format, however, it is not the only format under od command.
What is the od command in Linux?
od is short for Octal Dump and developers use this command to debug scripts in Linux. The main functionality of od command is that it helps in understanding data that otherwise is not readable for humans. If you have data in plaintext, you can always use the echo command.
Octal is a base-8 number system that can represent binary numbers and other numbers in a shorter form.
The different options to use along with od command are:
- -c: It displays the content in character format.
- -b: It displays the content in octal format.
- -An: It displays the content with no offset information.
- -j: It displays output after skipping some characters.
- -i: It displays the output as Decimal Integer.
- -x: It displays content as two-byte hexadecimal.
- -o: It displays content as two-byte octal.
The text for the file in examples below is:
Hello
World
!
Hello World
100
101
102
{
}
Using od command to display different formats
We can use the od command to display outputs in a variety of formats. Let’s see the different formats here.
1. Display as Characters
To display in character format use -c flag :
$ od -c filename.txt
We can see that this command displays all the characters. \n represents new line in the output. The first number in each row is the bit offset in file. Byte offset is the number of character that exists up until that point counting from the beginning of the file.
2. Display as Octal
To display content in octal format use -b flag along with od command.
$ od -b filename.txt
To understand the output better, combine it with -c flag. Doing so, shows both the outputs together.
$ od -bc filename.txt
The output acts like a conversion chart between the two formats.
3. Display as Decimal Integer
Using -i flag along with od command displays the output in decimal integer format.
$ od -i filename.txt
To understand the output better, combine it with -c flag.
$ od -ic filename.txt
4. Display Two byte hexadecimal
To display the content of the file in two byte hexadecimal use –x flag along with od command :
$ od -x filename.txt
Hexadecimal is base 16. To understand the output better, combine it with -c flag.
$ od -xc filename.txt
5. Display as Two byte octal
To display the output in two byte octal format use the -o flag along with od command.
$ od -o filename.txt
To understand this format better, we can combine it with -c and -b.
$ od -oc filename.txt
$ od -ob filename.txt
We can see that two bytes from regular octal output make one unit of two byte octal. Hence the name.
6. Display without byte offset
Byte offset is the first column in output by default. To display the output of od command without the byte offset use –An flag.
$ od -An filename.txt
-An flag works with other formats as well.
Here we use it with -c flag that displays output in character format.
$ od -An -c filename.txt
7. Skipping bytes in Display
To skip some bytes in the output, use the -j flag along with the number of bytes you want to skip.
$ od -j3 filename.txt
-j3 skips the first three bytes.
Let’s combine this with -c flag to make sense of the output.
$ od -j3 -c filename.txt
We can see that od command skips first 3 and 6 bytes respectively. We can even combine -An flag with -j flag.
$ od -An -j6 -c filename.txt
8. Displaying selective bytes
This operation is opposite to the one above. This displays only a particular number of bytes in the output. The flag for displaying selective bytes is -N.
$ od -An -N6 -c filename.txt
Only the first 6 and 11 bytes are there in the output respectively.
Conclusion
In this tutorial, we covered od command in Linux and the different formats that it can display a file in. Combining two flags together gives a better understanding of the output. For more information on od command, refer to its man page.