od command in Linux with Usage Examples

Written by: Bobbin Zachariah   |   Last updated: November 21, 2022

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
display in octal bytes

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
printable characters

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
skip bytes

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
customize the output

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
different format

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
limit bytes

Display in Decimal

The -i option will display the output as decimal integers. 

od -i input2.txt
output in decimal format

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
output format in hexadecimal 2-byte units

The output you can see as 2-byte units.

od command Options

Let's check some useful od options:

OptionsDescription
-a    same as -t a, select named characters, ignoring high-order bit
-bsame as -t o1, select octal bytes
-csame as -t c,  select printable characters or backslashescapes
-dsame as -t u2, select unsigned decimal 2-byte units
-fsame as -t fF, select floats
-isame as -t dI, select decimal ints
-lsame as -t dL, select decimal longs
-osame as -t o2, select octal 2-byte units
-ssame as -t d2, select decimal 2-byte units
-xsame 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.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment