od command

 

Command: od (Octal Dump)


🔍 Purpose

The od command is used to display file contents in various numeric formats, such as:

  • Octal (default)

  • Hexadecimal

  • Decimal

  • Character

  • ASCII

It allows you to view raw binary data — the exact bytes stored in memory or a file — which is extremely useful in:

  • Debugging programs

  • Examining binary or data files

  • Understanding file formats

  • Learning how text and numbers are represented internally


Basic Syntax

od [options] filename

Example:

od myfile.txt

🧠 Default Behavior

By default, od displays the file’s contents in octal format.

Example:

Create a simple file:

echo "OS" > test.txt

Run:

od test.txt

Output:

0000000 117 123 012 0000003

Explanation:

  • The first column (0000000, 0000003) → byte offset in the file

  • The numbers (117 123 012) → ASCII values of characters in octal

    • 117 = O

    • 123 = S

    • 012 = newline (\n)


🧩 Viewing in Different Formats

You can change how the bytes are displayed using options.


🔹 1. Hexadecimal Output

od -x test.txt

Output:

0000000 534f 000a 0000003

🧩 Explanation:

  • 534f = Hex representation of “OS” (O=4F, S=53)

  • 000a = newline (\n)


🔹 2. Character Output

od -c test.txt

Output:

0000000 O S \n 0000003

🧩 Explanation:
Displays the character equivalents of bytes — much easier to read for text files.


🔹 3. ASCII + Octal Together

od -a test.txt

Output:

0000000 O S nl 0000003

🧩 Explanation:

  • Shows ASCII names instead of characters (e.g., nl for newline).


🔹 4. Decimal Output

od -i test.txt

Output:

0000000 12335 00010 0000003

🧩 Displays integer (decimal) representation of the bytes.


🔹 5. Binary Output

od -b test.txt

Output:

0000000 117 123 012 0000003

🧩 Same as default — octal representation of bytes.


🔹 6. Hex Dump (Most Common Use)

od -t x1 test.txt

Output:

0000000 4f 53 0a 0000003

🧩 Explanation:

  • 4fO

  • 53S

  • 0a → newline
    Shows each byte in hexadecimal (x1 = one-byte units).


🔹 7. Combine Character and Hex

od -tx1 -c test.txt

Output:

0000000 4f 53 0a O S \n 0000003

🧩 Displays both — useful for teaching how ASCII characters map to byte values.


Viewing Specific Number of Bytes

Use the -N option:

od -c -N 5 myfile.txt

🧩 Displays only the first 5 bytes.


Skipping Bytes

Use the -j option to skip bytes from the beginning:

od -c -j 3 myfile.txt

🧩 Skips the first 3 bytes, then prints the rest.


Starting Offset

Use the -A option to control the address format in the first column:

od -A x -t x1 test.txt

Output:

000000 4f 53 0a 000003

🧩 -A x displays offsets in hexadecimal.


Display Memory or Binary Files

You can also examine binary files like compiled executables:

od -t x1 a.out | head

🧩 Displays the raw binary structure of the compiled program — great for understanding what’s actually stored.


Simple Lab Exercise

Aim:

To study the use of the od command for displaying file contents in octal, hexadecimal, and character formats.


Procedure:

  1. Create a text file:

    echo "Operating Systems" > sample.txt
  2. Display in different formats:

    od sample.txt od -c sample.txt od -x sample.txt od -t x1 sample.txt od -a sample.txt
  3. Observe and record differences in the output.


Sample Observation Table

CommandDescription    Output (example)
od sample.txtOctal representation    117 160 145 ...
od -c sample.txtCharacter view    O p e r ...
od -x sample.txtHexadecimal view    704f 6572 ...
od -t x1 sample.txtOne byte per hex value        4f 70 65 ...
od -a sample.txtASCII names    O p e r ...

🧠 Learning Outcomes

Students will:

  • Understand how characters and data are represented in memory.

  • Learn to interpret ASCII, octal, and hexadecimal representations.

  • Get a hands-on feel for binary-level file inspection.


Quick Reference Summary

OptionMeaning
-b            Display octal bytes
-c            Display ASCII characters
-a            Display named ASCII characters
-x            Display hexadecimal words
-t x1            Display hexadecimal bytes
-i            Display decimal integers
-N n            Display only n bytes
-j n            Skip n bytes from start
-A x            Display addresses in hex

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command