Linux online course

Using the od Command for Octal, Hexadecimal, Decimal, and Character Dumps in Linux

Learn how to use Linux od to inspect file bytes in octal, hexadecimal, signed decimal, and character-oriented formats.

The Linux od command displays file data as a formatted dump. Its name means octal dump, because its traditional default representation uses octal, or base 8. You can also use it to inspect data in hexadecimal, decimal, and character-oriented forms.

A byte dump is useful when a normal text viewer cannot clearly show a file's contents. This includes binary files, executable files, encoded data, and text containing invisible whitespace or control characters.

This lesson assumes basic terminal use, file-name arguments, and the idea that files consist of bytes. For related file inspection, see how to determine a file's type.

What od displays

A byte is a basic unit of file data, typically containing eight bits. The od command reads those bytes and writes a formatted representation of them. The representation may use numbers or visible characters, depending on the option you select.

An offset is a position within the input file. In an od listing, the offset tells you where the displayed data begins. It is not a text line number.

Unlike a normal text viewer, od makes invisible or non-human-readable data explicit. For example, it can help reveal a newline at the end of a file, a tab between fields, or bytes inside an executable.

Create a small file for inspection

Use printf to create a predictable example:

printf 'Hello Linux\n' > sample.txt

The file contains the visible characters Hello Linux followed by a newline. The space and newline are both bytes, even though the newline moves the cursor to the next line rather than displaying as a visible symbol.

Default octal output

Run od with a file name and no format option:

od sample.txt

A typical result looks similar to this:

0000000 110145 154154 157040 114151 156165 170012
0000014

Exact spacing and grouping can vary between implementations, but the output has three important ideas:

  • The leading value is an offset into the file.
  • The values to its right are formatted data read from the file.
  • The final offset indicates how much input was processed.

Octal is a base-8 numbering system using digits from 0 through 7. The default offset is also expressed in octal. Therefore, 0000014 means octal 14, which is decimal 12: the number of bytes in Hello Linux\n.

The default display groups bytes into output units rather than necessarily showing one separate number for every byte. For example, an octal value can represent two adjacent bytes as one displayed word. The underlying bytes have not changed; only their presentation has.

How to read an od row

Output componentMeaningNotes
Leading offsetThe position where the row's data beginsWith the default format, the offset is in octal.
Formatted data valuesBytes from the file represented in the selected formatValues may be grouped into multi-byte output units.
Final offsetThe total amount of input processedConvert the base if you want to compare it with a decimal file size.

Connecting characters to byte values

For the sample file, the visible text includes H, e, l, l, o, a space, and the letters in Linux. The final newline is a control character. In a numeric dump, each byte is represented by a number; in a character dump, printable bytes are shown as characters and the newline is shown symbolically.

Do not interpret an octal value as decimal. For example, the octal byte value for the character A is 101, while its decimal value is 65 and its hexadecimal value is 41.

Hexadecimal display with -x

Hexadecimal is a base-16 numbering system. It uses the digits 0 through 9 and the letters A through F. Hexadecimal is common in binary formats, byte encodings, memory-related debugging, and program data because each hexadecimal digit represents four bits.

od -x sample.txt

The -x option renders the input as hexadecimal words. Compare this output with the default octal output: both commands read exactly the same file bytes, but they use different bases and grouping.

Hexadecimal is often a convenient choice when comparing a file with a specification that documents byte values in hex or when looking for recognizable binary signatures.

Decimal display with -i

Decimal is the familiar base-10 numbering system. Use -i to request signed decimal integer output:

od -i sample.txt

The -i format displays signed decimal integer units. It is not necessarily a simple one-number-per-byte character-code view. The selected output type affects grouping and interpretation, but it does not alter the file or its underlying bytes.

Character display with -c

Use -c for a character-oriented view:

od -c sample.txt

A typical result resembles:

0000000   H   e   l   l   o       L   i   n   u   x  \n
0000014

Printable characters are shown directly where possible. A printable character is a character that can normally be displayed visibly, such as a letter, digit, or punctuation mark. Non-printing bytes use escape-style notation or symbolic output.

  • A space appears as an empty-looking gap, so inspect spacing carefully.
  • A tab is commonly represented as \t.
  • A newline is commonly represented as \n.
  • A carriage return may be represented as \r.

A control character is a non-printing character used for an operation such as a newline, tab, or carriage return. Character format is especially useful for finding whitespace and line-ending differences that are hard to see in ordinary output.

Compare od output formats

OptionOutput representationNumeric base or interpretationBest use case
No optionDefault octal outputOctal, with implementation-defined grouping such as wordsTraditional numeric dumps and offset-oriented inspection
-xHexadecimal wordsBase 16Binary formats, byte encodings, and program data
-iSigned decimal integersBase 10 with signed integer interpretationInspecting numeric data when integer grouping is appropriate
-cCharacter-oriented outputPrintable characters plus special notation for control bytesRecognizing text, spaces, tabs, and newlines

These are different views of the same input. Choosing a format does not convert or rewrite the file. It only changes how od presents the bytes.

Choosing the right representation

  • Choose the default octal format when you specifically need octal values or are working with a tool or document that uses octal.
  • Choose -x for byte-level debugging, binary signatures, and data commonly documented in hexadecimal.
  • Choose -i when the input should be considered signed integer data. Remember that its units may contain multiple bytes.
  • Choose -c when you need to recognize text and expose whitespace or control characters.

For a first investigation of an unfamiliar file, od -c can make readable portions understandable, while od -x is usually more convenient for detailed binary inspection.

Shell aliases for frequent dumps

An alias is a shell-defined shortcut that expands to a longer command. If you frequently need a character-oriented dump, define a temporary alias:

alias odc='od -c'
odc sample.txt

This alias exists only in the current shell session. Closing the terminal or starting a separate shell normally removes it.

For Bash, you can append the alias to the Bash startup file:

printf "alias odc='od -c'\n" >> ~/.bashrc
source ~/.bashrc

Use the startup file appropriate to your active shell. The source command reloads the Bash configuration in the current shell; alternatively, open a new terminal.

For more Bash background, see Bourne Again Shell Bash.

od is not a line-numbering command

od reports byte offsets, not logical text-line numbers. If the goal is to number lines in a text file, use a text-processing tool:

nl -ba sample.txt
cat -n sample.txt
TaskRecommended commandWhat is counted or displayed
Inspect bytes numerically or as charactersod -c FILE or another suitable od formatByte data and byte offsets
Number all text linesnl -ba FILEEvery line, including blank lines
Number nonblank text linescat -n FILEText lines with line numbers; blank lines are handled differently from nl -ba

Line numbering and byte inspection complement each other. A line number identifies a logical text line, while an offset identifies a byte position.

Troubleshooting od output

The output does not look like the original text

You are viewing numeric byte values rather than plain text. Try od -c FILE for recognizable characters, or use a normal text viewer when byte-level inspection is unnecessary.

Offsets seem unexpected

The default offsets are octal, not decimal. Convert them before comparing them with decimal positions or a reported file size. The final offset is also displayed in the selected offset representation.

A newline, tab, or space seems missing

Whitespace is not always visually obvious. Use od -c FILE and look for notation such as \n or \t. A space may still appear as a gap, so compare the character output with the numeric output when necessary.

Decimal output has unexpected grouping

-i displays signed decimal integer units, not necessarily one decimal value per byte. Use -c for character recognition, or select a representation whose grouping matches your inspection goal.

An alias is unavailable in a new terminal

The alias was defined only in the previous shell. Add it to the appropriate startup file, reload that file, and check the spelling with alias odc.

You expected line numbers

od uses byte offsets. Use nl -ba FILE or cat -n FILE when you need text-line numbering.

Key points

  • od means octal dump and displays formatted file data.
  • With no format option, od FILE uses octal output, including octal offsets.
  • An offset is a byte position, not a line number.
  • od -x uses hexadecimal words, od -i uses signed decimal integers, and od -c shows printable characters and control-character notation.
  • Different formats show the same bytes in different representations.
  • Use aliases for repeated commands, and use nl or cat -n for line numbering.