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
Example:
🧠 Default Behavior
By default, od displays the file’s contents in octal format.
Example:
Create a simple file:
Run:
Output:
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
Output:
🧩 Explanation:
-
534f= Hex representation of “OS” (O=4F,S=53) -
000a= newline (\n)
🔹 2. Character Output
Output:
🧩 Explanation:
Displays the character equivalents of bytes — much easier to read for text files.
🔹 3. ASCII + Octal Together
Output:
🧩 Explanation:
-
Shows ASCII names instead of characters (e.g.,
nlfor newline).
🔹 4. Decimal Output
Output:
🧩 Displays integer (decimal) representation of the bytes.
🔹 5. Binary Output
Output:
🧩 Same as default — octal representation of bytes.
🔹 6. Hex Dump (Most Common Use)
Output:
🧩 Explanation:
-
4f→O -
53→S -
0a→ newline
Shows each byte in hexadecimal (x1= one-byte units).
🔹 7. Combine Character and Hex
Output:
🧩 Displays both — useful for teaching how ASCII characters map to byte values.
Viewing Specific Number of Bytes
Use the -N option:
🧩 Displays only the first 5 bytes.
Skipping Bytes
Use the -j option to skip bytes from the beginning:
🧩 Skips the first 3 bytes, then prints the rest.
Starting Offset
Use the -A option to control the address format in the first column:
Output:
🧩 -A x displays offsets in hexadecimal.
Display Memory or Binary Files
You can also examine binary files like compiled executables:
🧩 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:
-
Create a text file:
-
Display in different formats:
-
Observe and record differences in the output.
Sample Observation Table
| Command | Description | Output (example) |
|---|---|---|
od sample.txt | Octal representation | 117 160 145 ... |
od -c sample.txt | Character view | O p e r ... |
od -x sample.txt | Hexadecimal view | 704f 6572 ... |
od -t x1 sample.txt | One byte per hex value | 4f 70 65 ... |
od -a sample.txt | ASCII 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
| Option | Meaning |
|---|---|
-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
Post a Comment