xxd command
Command: xxd (Hex Dump Tool)
๐ Purpose
The xxd command creates a hexadecimal (hex) dump of a file or standard input.
It is used to:
-
Display the binary contents of a file in human-readable hex format
-
Show the corresponding ASCII characters
-
Recreate the original file from the hex dump (reverse operation)
This makes it extremely useful for:
-
Studying how data is stored in files
-
Debugging binary data
-
Understanding file formats
-
Working with encrypted or compiled data
๐งฉ Basic Syntax
Example:
๐ Example 1: Simple Text File
Create a file:
Now run:
Output:
๐งฉ Explanation
| Column | Meaning |
|---|---|
00000000 | Byte offset (address) |
4f53 204c 6162 0a | Hexadecimal representation of file content |
OS Lab. | ASCII characters (the rightmost column) |
Each pair of hex digits (like 4F) represents one byte.
So:
-
4F→ O -
53→ S -
20→ space -
4C→ L -
61→ a -
62→ b -
0A→ newline
๐ Example 2: Display in Hex Only
If you want only the hex output (no ASCII):
Output:
๐งฉ This gives a plain hex stream, often used for data transmission or comparison.
๐ Example 3: Reverse a Hex Dump
xxd can recreate the original file from its hex dump.
-
Create a hex dump and save it:
-
Recreate the original file:
-
Check:
Output:
✅ This round-trip conversion makes xxd an excellent teaching tool for showing how text translates to binary and back.
๐ Example 4: Specify Bytes per Line
By default, xxd shows 16 bytes per line.
To change that:
Output:
๐งฉ Shows 8 bytes per line instead of 16 — useful for short files.
๐ Example 5: Display Only Specific Bytes
Show the first 8 bytes:
Skip the first 4 bytes:
๐งฉ Options:
-
-l <n>→ show n bytes -
-s <n>→ skip n bytes
๐ Example 6: Combine with a Command
You can use xxd on the output of another command.
For example:
Output:
๐งฉ Example 7: View a Binary File
Try this on an executable:
Output (truncated):
๐งฉ Explanation:
-
The first 4 bytes
7f 45 4c 46correspond to.ELF— the magic number that identifies an ELF (Executable and Linkable Format) binary in Linux.
This is a great demo for students — it connects the concept of magic numbers and file identification (as seen with the file command).
๐ Example 8: Change Output Format
Show offsets in decimal instead of hex:
๐งฉ Common Options Summary
| Option | Description |
|---|---|
-p | Plain hex dump (no offsets or ASCII) |
-r | Reverse operation (hex → binary) |
-c <n> | Show n bytes per line |
-s <n> | Skip n bytes from start |
-l <n> | Limit to n bytes |
-A | Display addresses in decimal |
-g <n> | Group bytes (1, 2, or 4) |
-e | Switch to little-endian mode for multibyte groups |
๐งช Simple Lab Exercise
Aim:
To study the use of the xxd command for viewing and converting file contents in hexadecimal format.
Procedure:
-
Create a sample text file:
-
Generate hex dump:
-
View plain hex:
-
Reverse the hex dump:
-
Observe and compare the outputs.
Sample Observation Table
| Command | Description | Sample Output |
|---|---|---|
xxd oslab.txt | Hex + ASCII dump | 4f53 204c 6162 → OS Lab |
xxd -p oslab.txt | Plain hex | 4f53204c6162 |
xxd -r dump.hex new.txt | Reverse hex dump | Text restored |
xxd -c 8 oslab.txt | 8 bytes per line | Neater view |
๐ง Learning Outcomes
Students will:
-
Understand how text and binary data are represented in hexadecimal form.
-
Learn how to interpret ASCII codes and bytes visually.
-
Learn how to reverse hex dumps back into files.
-
See the link between file structure and binary representation.
๐งพ Quick Reference
| Command | Purpose |
|---|---|
xxd file | Create hex dump |
xxd -p file | Plain hex (no ASCII) |
xxd -r file | Reverse hex dump |
xxd -l n file | Show n bytes |
xxd -s n file | Skip n bytes |
xxd -c n file | Show n bytes per line |
๐งฉ Teaching Tip
For demonstrations, let students use:
and explain how:
-
41→A -
42→B
This helps them visualize ASCII ↔ hex ↔ character mapping
Comments
Post a Comment