od Command in Linux



The od command in Linux dumps files in octal and other formats, such as hexadecimal, ASCII, and more. It is a handy command, especially for debugging binary files.

By default, the od command displays the contents of the specified file in octal. It treats multiple streams as a single file; if a dash (-) is specified, then it reads from standard input.

Table of Contents

Here is a comprehensive guide to the options available with the od command −

Syntax of od Command

The syntax of the od command is as follows −

od [options] [file]

The [options] field in the above syntax is used to specify options such as various output formats. The [file] is used to specify the file that needs to be displayed in the specified format.

od Command Options

The options of the od command are listed below −

Flags Options Description
-A --address-radix=RADIX Choose the base for file offsets [doxn](d for decimal, o for octal, x for hexadecimal, n for none)
-j --skip-bytes=BYTES Skip the first BYTES of input
-N --read-bytes=BYTES Limit output to BYTES of input
-S --strings[=BYTES] Display strings of at least BYTES printable characters [default is 3]
-t --format=TYPE Select output format(s) (x for hexadecimal, c for ASCII)
-v --output-duplicates Show all lines, avoiding * for repeated lines
-w --width[=BYTES] Set line width to BYTES [default is 16]
--traditional Enable traditional argument handling
--help Show usage help and exit
--version Display version info and exit

The shorthand of various format types is listed below −

Types Equivalent to Description
-a -t a Displays named characters (ignores the high-order bit)
-b -t o1 Displays octal bytes
-c -t c Displays printable characters or backslash escapes
-d -t u2 Displays unsigned decimal 2-byte units
-f -t fF Displays floating-point numbers
-i -t dI Displays signed decimal integers
-l -t dL Displays signed decimal long integers
-o -t o2 Displays octal 2-byte units
-s -t d2 Displays signed decimal 2-byte units
-x -t x2 Displays hexadecimal 2-byte units

The above formats can be specified directly or by using the -t option.

Examples of od Command in Linux

In this section, the usage of od command will be discussed with examples. The context of the text file used in the following examples is shown below −

od Command in Linux1

Displaying File Contents to Octal Bytes

By default, the od command displays the contents of the specified input in octal byte format. To display the contents of a text file in octal bytes, use the following command −

od file.txt
od Command in Linux2

Displaying File Contents in Hexadecimal

To display the file contents in hexadecimal, use the -x option −

od -x file.txt
od Command in Linux3

Displaying File Contents in Floating Point Numbers

To display the file contents in floating point numbers, use the -f option −

od -f file.txt
od Command in Linux4

Displaying File Contents with Named Characters

Named characters are symbolic representations of characters used in text encoding systems, specifically those that are non-printable or control characters. To display the named characters of a file, use the -a option −

od -a file.txt
od Command in Linux5

This mode helps in identifying non-printable characters by showing them with their symbolic names, which makes it easier to understand control sequences in the file. Common named characters are listed below −

  • sp = space
  • nl = newline \n
  • tab = \t
  • cr = carriage return \r
  • bs = backspace
  • esc = escape \e

Displaying Printable Characters with Escape Sequences

To show printable characters and escape sequences, use the -c option with the od command −

od -c file.txt
od Command in Linux6

Skipping Bytes

To skip bytes, use the -j or --skip-bytes option with the number of bytes. For example, to skip the first 7 bytes, use the following command −

od -j 7 -c file.txt
od Command in Linux7

Reading Specific Number of Bytes

To read a specific number of bytes, use the -N or --read-bytes option with the number of bytes. For example, to read the first 6 bytes, use the od command in the following way −

od -N 6 -c file.txt
od Command in Linux8

The -N option limits the number of bytes read from the file.

Displaying a Specific Number of Bytes Per Line

To display a specific number of bytes per line, use the -w or --width option with the od command. To display 7 bytes per output line, use the od command in the following way −

od -w7 -c file.txt
od Command in Linux9

Saving the Output to a File

To save the displayed output to a file, use the redirection operator (>) with the file name.

od file.txt > output.txt
od Command in Linux10

Getting Input from the Command Line

To specify input in the command line, use the dash (-) after the od command and option −

od -x -

First type the command and press Enter −

od Command in Linux11

Type the string to be converted and press Enter. Lastly, press the ctrl+d to display the output as shown below −

od Command in Linux12

Changing File Offset Base

To change the file offset base, use the -A or --address-radix option with [doxn]. The d is for decimal, o for octal, x for hexadecimal, and n for none.

od -A d file.txt
od Command in Linux13

Conclusion

The od command in Linux is a handy tool for displaying file contents in various formats, including octal, hexadecimal, and ASCII. It offers options to skip bytes, limit output, adjust line width, and convert data into different formats, making it essential for analyzing binary files and debugging.

In this tutorial, we explained the od command, its syntax, options and usage in Linux with examples.

Advertisements