Linux od Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn about the od (octal dump) command in Linux, which is used to display the contents of a file in octal, decimal, hexadecimal, or other formats. You will start by understanding the basic usage of the od command, then explore various options to customize the output format, and finally perform a hexadecimal dump of files. This lab covers the essential skills for basic file and directory operations in a Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/SystemInformationandMonitoringGroup -.-> linux/dd("`File Converting/Copying`") subgraph Lab Skills linux/cat -.-> lab-422848{{"`Linux od Command with Practical Examples`"}} linux/echo -.-> lab-422848{{"`Linux od Command with Practical Examples`"}} linux/dd -.-> lab-422848{{"`Linux od Command with Practical Examples`"}} end

Understand the od Command

In this step, you will learn about the od (octal dump) command in Linux. The od command is used to display the contents of a file in octal, decimal, hexadecimal, or other formats.

To get started, let's create a sample file with some text:

echo "Hello, Linux!" > sample.txt

Now, you can use the od command to view the contents of the file in hexadecimal format:

od -x sample.txt

Example output:

0000000 4865 6c6c 6f2c 2020 4c69 6e75 7821 0a00
0000020

The -x option tells od to display the file contents in hexadecimal format. Each line shows the offset (in bytes) from the beginning of the file, followed by the hexadecimal representation of the data.

You can also use the -t option to specify the output format. For example, to display the file contents in decimal format:

od -t d1 sample.txt

Example output:

0000000   72  101  108  108  111  44     32  108  105  110  117  120  33  10
0000020

The d1 argument tells od to display the data in 1-byte decimal format.

Explore od Command Options

In this step, you will explore the various options available with the od command to customize the output format.

First, let's create another sample file with some binary data:

echo -e "\x01\x02\x03\x04\x05\x06\x07\x08" > binary.txt

Now, you can use the od command with different options to display the file contents in various formats:

## Display in octal format
od -t o1 binary.txt

## Display in hexadecimal format
od -t x1 binary.txt

## Display in ASCII format
od -t c binary.txt

Example output:

0000000 001 002 003 004 005 006 007 010
0000010
0000000 01 02 03 04 05 06 07 08
0000010
0000000 001 002 003 004 005 006 007 010
0000010

The -t option allows you to specify the output format. In the examples above, o1 represents 1-byte octal, x1 represents 1-byte hexadecimal, and c represents character (ASCII) format.

You can also combine multiple output formats using the -t option:

od -t x1 -t d1 -t c binary.txt

Example output:

0000000 01 02 03 04 05 06 07 08
0000010

This will display the file contents in hexadecimal, decimal, and character formats side-by-side.

Perform Hexadecimal Dump of Files

In this final step, you will learn how to use the od command to perform a hexadecimal dump of files.

Let's create a larger sample file with some random data:

dd if=/dev/urandom of=random.txt bs=1024 count=10

This will create a file named random.txt with 10 kilobytes of random data.

Now, you can use the od command to display the hexadecimal dump of the file:

od -x random.txt

Example output:

0000000 f6d1 9d2e 4a4b 5b8f 6acd 0f1e 0f6a 1b2e
0000020 b6f9 2d4a 1e0f 6a1b 2e6f 9d2e 4a4b 5b8f
0000040 6acd 0f1e 0f6a 1b2e b6f9 2d4a 1e0f 6a1b
0000060 2e6f 9d2e 4a4b 5b8f 6acd 0f1e 0f6a 1b2e
0000100 b6f9 2d4a 1e0f 6a1b 2e6f 9d2e 4a4b 5b8f
0000120 6acd 0f1e 0f6a 1b2e b6f9 2d4a 1e0f 6a1b
0000140 2e6f 9d2e 4a4b 5b8f 6acd 0f1e 0f6a 1b2e
0000160 b6f9 2d4a 1e0f 6a1b 2e6f 9d2e 4a4b 5b8f
0000200 6acd 0f1e 0f6a 1b2e b6f9 2d4a 1e0f 6a1b
0000220 2e6f 9d2e 4a4b 5b8f 6acd 0f1e 0f6a 1b2e
0000240

The output shows the hexadecimal representation of the file contents, with each line displaying the offset (in bytes) from the beginning of the file.

You can also use the -c option to display the file contents in ASCII format alongside the hexadecimal dump:

od -tx1 -tc random.txt

Example output:

0000000 f6 d1 9d 2e 4a 4b 5b 8f 6a cd 0f 1e 0f 6a 1b 2e  |......JK[..j...j..|
0000020 b6 f9 2d 4a 1e 0f 6a 1b 2e 6f 9d 2e 4a 4b 5b 8f  |..-J..j..o..JK[.|
0000040 6a cd 0f 1e 0f 6a 1b 2e b6 f9 2d 4a 1e 0f 6a 1b  |j....j....-J..j.|
0000060 2e 6f 9d 2e 4a 4b 5b 8f 6a cd 0f 1e 0f 6a 1b 2e  |.o..JK[.j....j..|
0000100 b6 f9 2d 4a 1e 0f 6a 1b 2e 6f 9d 2e 4a 4b 5b 8f  |..-J..j..o..JK[.|
0000120 6a cd 0f 1e 0f 6a 1b 2e b6 f9 2d 4a 1e 0f 6a 1b  |j....j....-J..j.|
0000140 2e 6f 9d 2e 4a 4b 5b 8f 6a cd 0f 1e 0f 6a 1b 2e  |.o..JK[.j....j..|
0000160 b6 f9 2d 4a 1e 0f 6a 1b 2e 6f 9d 2e 4a 4b 5b 8f  |..-J..j..o..JK[.|
0000200 6a cd 0f 1e 0f 6a 1b 2e b6 f9 2d 4a 1e 0f 6a 1b  |j....j....-J..j.|
0000220 2e 6f 9d 2e 4a 4b 5b 8f 6a cd 0f 1e 0f 6a 1b 2e  |.o..JK[.j....j..|
0000240

This output displays the hexadecimal values alongside their corresponding ASCII characters, making it easier to interpret the file contents.

Summary

In this lab, you learned about the od (octal dump) command in Linux, which is used to display the contents of a file in various formats, including octal, decimal, and hexadecimal. You created sample files with text and binary data, and explored the different options available with the od command to customize the output format. You learned how to use the -t option to specify the desired output format, such as 1-byte octal, 1-byte hexadecimal, and character (ASCII) format. Additionally, you discovered how to combine multiple options to further customize the output.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like