There may be times when you need to debug certain log files or texts from one format or technically defined as number systems, to another. Luckily, Linux already has a built program to convert one number system to another.
In this tutorial, we learn about od command in Linux with examples.
od command
“Od” which stands for octal dump, is a command found in most Linux Distros which translates input files with texts, strings or numbers and dumps it to the desired format.
The default format is in octal format, however, you can output signed and unsigned hexadecimals, ASCII characters, IEEE binary floating point, among others. You can use certain combinations of options to generate various formats with the requested representation of each byte vertically aligned.
The primary purpose of the conversion is not just to make certain log files human-readable, but the same is true for when compiling certain applications that needs to be communicated to a specific format such as octal or hexadecimal.
Syntax:
od [OPTION]... [FILE]...
Installation
The Linux od command should be already natively installed in your Linux system, however, in case it is not found, you can install it using the following instructions:
Debian / Ubuntu
sudo apt install coreutils
Fedora
sudo dnf install coreutils
Redhat
sudo yum install coreutils
Arch Linux
sudo pacman install coreutils
How to use od command
In this part of the tutorial, we will show you how to use the od Linux command to convert certain inputs such as a text file to a number of different number formats.
It is important that before we start with the Linux od command options, we need to create a text file with alphanumeric and character content to be able to understand how the command works. In the following examples, we have created a file named input.txt and added some data.
Use the cat command to display your initial text file before we execute the od command.
cat input.txt
Display File in Octal
In this section, we will use the Linux od command to print the input file content in octal format. To begin, use the -b option in the command line:
od -b input.txt
In this example, the previous contents of the input file, bearing numbers and some human-readable words, was converted into the octal number system. The initial column in the output data of od represents the byte offset in the said file.
Next, we will be demonstrating on how to print the input file into character format. To begin, affix the -c option in the command line:
od -c input.txt
In character output, the numbers and words are separated by the \n, meaning a new line took place.
The -A[x] flag will output the format for file offsets depending on which variable you replace x with. For example, affixing -An with -c option will print the input.txt file content in character format but with no offset information.
od -An -c input.txt
Skip Bytes
You can also skip a certain number of bytes when generating your conversion display output data. This is achieved by using the -j option. To illustrate this clearly, consider another set of input2 text file.
od -j4 -c input2.txt
In this example, the initial 4 bytes were skipped from the output. The output started with the octal system 010 and had skipped 001.
Limit the output width
You can also limit the output width being displayed by od. The correct variable for this is -w. It limits the output BYTES bytes per output line. When no value is specified, then the default 32 BYTES becomes the implied value.
od -w3 -An -c input2.txt
In this example, we’ve limited the number of column displays to 3, as such the output only displays the indicated number of column widths.
Display different format - Hex , Octal and Decimal
You can also set the display to the different formats available on od, whether hex, octal or decimal. The variable set is the -A option or --address-radix=[value]. It outputs format for file offsets depending on the value. The value is either one of d for the Decimal format, o for Octal format, x for the Hexadecimal format or n for None.
Format for Decimal
od -Ad -c input2.txt
Format for Octal
od -Ao -c input2.txt
Format for Hexadecimal
od -Ax -c input2.txt
As you can see, the contents of the input2.txt file is displayed in different formats by concatenating some special character with -A.
Limit the bytes
You can also limit the display bytes in output by using the -N option. You should be careful with this option when working with double-byte characters. If od is processing a double-byte character when it encounters the nth byte and this byte is not the last byte of the character, od displays ??? instead of the character.
For example to show only the initial 4 bytes of the file:
od -N4 -c input2.txt
Display in Decimal
The -i option will display the output as decimal integers.
od -i input2.txt
In this example, the numeric integers were converted into decimal format.
Hexadecimal with 2 bytes
The -x option will display the output as hexadecimal integers with 2 bytes.
od -x input2.txt
The output you can see as 2-byte units.
od command Options
Let's check some useful od options:
Options
Description
-a
same as -t a, select named characters, ignoring high-order bit
-b
same as -t o1, select octal bytes
-c
same as -t c, select printable characters or backslashescapes
-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
Conclusion
The Linux od command is especially useful when debugging scripts for any unwanted changes in the contents or the characters. We have demonstrated the flexibility of the od command which has been commonly used throughout the Unix and Linux systems. od provides more formatting options when compared to hexdump and xxd.
In this tutorial, we have illustrated on how to use the od command through the Linux terminal and summarized the different options to convert data into octal and other formats.
You can explore more of the Linux od command by trying it firsthand with your own Linux device. In this way, you will be able to learn and appreciate its usefulness and versatility.
Navigate all-in-one place of Linux Commands for more learning.
If this resource helped you, let us know your care by a Thanks Tweet.
Did you find this article helpful?
We are glad you liked the article. Share with your friends.
About The Author
Bobbin Zachariah
Bobbin is a seasoned IT professional with over two decades of experience. He has excelled in roles such as a computer science instructor, Linux system engineer, and senior analyst. Currently, he thrives in DevOps environments, focusing on optimizing efficiency and delivery in AWS Cloud infrastructure. Bobbin holds certifications in RHEL, CCNA, and MCP, along with a Master's degree in computer science. In his free time, he enjoys playing cricket, blogging, and immersing himself in the world of music.
Comments