The Wayback Machine - https://web.archive.org/web/20240910223344/https://www.geeksforgeeks.org/od-command-linux-example/
Open In App

od command in Linux with example

Last Updated : 10 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

od command1

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:

od command2

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:

od command3

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:

od command4

5. – Option: Accept Input from the Command Line

Accept input from command line.

Syntax:

 $ od -c -

Example:

od command5

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:

joption

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:

Noption

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:

-woption

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:

-voption

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:

-ioption

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:

-ooption

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:

-xoption

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:

-toption

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:

helpoption

16. –version Option: Display Version Information

Shows the version of the ‘od’ command installed on your system.

Syntax:

$ $ od --version

Output:

versionoption

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.

od command in Linux – FAQs

What is the ‘od’ command used for in Linux?

The od (octal dump) command is used to display file contents in various formats such as octal, hexadecimal, decimal, and ASCII. It’s useful for inspecting non-human-readable data, debugging scripts, and analyzing binary files.

What is the default format used by the ‘od’ command?

The default format used by the ‘od’ command is octal.

How can I display file contents in hexadecimal format using ‘od’?

To display file contents in hexadecimal format, use the ‘-x’ option:

od -x input.txt

How do I view the contents of a file in ASCII characters using ‘od’?

Use the ‘-c’ option to display the file contents in ASCII character format:

od -c input.txt


Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Linux sftp command with Example
In this article, we are going discuss about sftp. It is a protocol for securely transferring files from a remote server to a local machine. before SFTP, FTP was used to transfer files but it was unsecured. An attacker can read the communication between a remote server and a local machine. What is SFTP?SFTP stands for Secure File Transfer Protocol.
5 min read
Linux setfacl command with Example
In Linux distribution, setfacl stands for Set File Access Control List. It is a command utility tool for setting access control lists in files and directories. setfacl is a powerful tool for managing file permission based on respective Users and Groups rather than general file permission. What is an Access Control List?Access Control List is a set
6 min read
while Command in Linux with Example
The "while" command is a powerful tool in the Linux shell that allows you to execute a series of commands repeatedly based on a given condition. It is commonly used in shell scripts to create loops, where a block of code is executed as long as a particular condition remains true. Basic Syntax of While CommandThe basic syntax of the "while" command
6 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
help Command in Linux with examples
If you are new to LINUX operating system and having trouble dealing with the command-line utilities provided by LINUX then you really need to know first of all about the help command which as its name says help you to learn about any built-in command. help command as told before just displays information about shell built-in commands. Here's the sy
3 min read
rcp Command in Linux with examples
There comes a time while using LINUX when there is a need to copy some information stored in a file to another computer. This can be done simply using rcp command line utility . Obviously there exists some other methods to complete the above mentioned task which are more secure (like scp or rsync) but this command lets you do this in the simple way
4 min read
du Command in LINUX
While working on LINUX, there might come a situation when you want to transfer a set of files or the entire directory. In such a case, you might wanna know the disk space consumed by that particular directory or set of files. As you are dealing with LINUX, there exists a command line utility for this also which is du command that estimates and disp
9 min read
dmesg command in Linux for driver messages
dmesg command also called as "driver message" or "display message" is used to examine the kernel ring buffer and print the message buffer of kernel. The output of this command contains the messages produced by the device drivers. Usage of dmesg : When the computer boots up, there are lot of messages(log) generated during the system start-up. So you
5 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax
4 min read
unexpand command in Linux with Examples
To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command. The unexpand command by default convert each spaces into tabs writing the produced output to the standard output. Here's the syntax of unexpand command : Syntax : $unexpand [OPTION]... [FILE]... where, OPTION refers to the options compatib
2 min read
uniq command in Linux with examples
The linux uniq command is basically used to remove all repeated lines in a file. This command is used when a line is repeated multiple times and replace these multiple lines with one line. This command is designed to work on sorted files. Purpose : Remove repetitious lines from the sorted 'input-file' and send unique lines to the 'output-file'. If
2 min read
anacron command in linux with examples
anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the j
3 min read
SED command in Linux | Set 2
We have discussed some of the SED command options in Sed Command in Linux/Unix with examples SED is used for finding, filtering, text substitution, replacement and text manipulations like insertion, deletion search, etc. It's a one of the powerful utilities offered by Linux/Unix systems. We can use sed with regular expressions. I hope atleast you h
8 min read
enable and disable command in Linux
Enables and disables are the built-in shell commands. enable command is used to start the printers or classes whereas the disable command is used to stop the printers or classes. Syntax For enable Command: enable [-a] [-dnps] [-f filename][name ...] Syntax For disable Command: disable [-c] [-W] [ -r [ reason ] ] printer Options: -a : List all built
1 min read
for command in Linux with Examples
for command in Linux is used to repeatedly execute a set of command for every element present in the list. Syntax: for NAME [in WORDS ... ] ; do COMMANDS; done Example: Options: help for : It displays help information.
1 min read
autoheader command in Linux with Examples
autoheader command in Linux is used to create a template file of C "#define" or any other template header for configure to use. If the user will give autoheader an argument, it reads the standard input instead of reading configure.ac and also writes the header file to the standard output. This command scans the configure.ac file and figures out whi
1 min read
expect command in Linux with Examples
expect command or scripting language works with scripts that expect user inputs. It automates the task by providing inputs. // We can install expect command using following if not installed // On Ubuntu $sudo apt install expect // On Redhat based systems $ yum install expect First we write a script that will be needing inputs from users and then we
2 min read
function command in Linux with examples
The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the user's current shell session or saved in the shell
2 min read
fg command in Linux with examples
fg command in linux used to put a background job in foreground. Syntax: fg [job_spec] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this job if n
1 min read
bg command in Linux with Examples
bg command in linux is used to place foreground jobs in background. Syntax: bg [job_spec ...] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this
1 min read
Clear Command in Linux with Examples
clear is a standard Unix computer operating system command that is used to clear the terminal screen. This command first looks for a terminal type in the environment and after that, it figures out the terminfo database for how to clear the screen. And this command will ignore any command-line parameters that may be present. Also, the clear command
2 min read
Continue Command in Linux with examples
continue is a command which is used to skip the current iteration in for, while, and until loop. It is used in scripting languages and shell scripts to control the flow of executions. It takes one more parameter [N], if N is mentioned then it continues from the nth enclosing loop. The syntax for the `continue` command in Linuxcontinue or continue [
2 min read
hostid command in Linux with examples
hostid is a command in Linux that is used to display the Host's ID in hexadecimal format. It provides a quick and straightforward way to retrieve the host ID, allowing administrators to associate it with software licenses or perform system-specific operations. Syntax of `hostid` command in Linuxhostid [OPTION] Options provided by hostedhostid --hel
1 min read
Dirname Command in Linux with Examples
dirname is a command in Linux that is used to remove the trailing forward slashes "/" from the NAME and prints the remaining portion. If the argument NAME does not contain the forward slash "/" then it simply prints dot ".". In other words, we can say that the `dirname` command is a useful tool for extracting the directory portion from a given path
2 min read
sdiff command in Linux with Examples
sdiff command in linux is used to compare two files and then writes the results to standard output in a side-by-side format. It displays each line of the two files with a series of spaces between them if the lines are identical. It displays greater than sign if the line only exists in the file specified by the File2 parameter, and a | (vertical bar
2 min read
Groups Command in Linux With Examples
In Linux, there can be multiple users (those who use/operate the system), and groups are nothing but a collection of users. Groups make it easy to manage users with the same security and access privileges. A user can be part of different groups. The `groups` is a powerful tool that helps administrators to view the group memberships of a user and re
2 min read