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

xxd [options] filename

Example:

xxd sample.txt

๐Ÿ“˜ Example 1: Simple Text File

Create a file:

echo "OS Lab" > sample.txt

Now run:

xxd sample.txt

Output:

00000000: 4f53 204c 6162 0a OS Lab.

๐Ÿงฉ Explanation

ColumnMeaning
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):

xxd -p sample.txt

Output:

4f53204c61620a

๐Ÿงฉ 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.

  1. Create a hex dump and save it:

    xxd sample.txt > dump.hex
  2. Recreate the original file:

    xxd -r dump.hex newfile.txt
  3. Check:

    cat newfile.txt

    Output:

    OS Lab

✅ 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:

xxd -c 8 sample.txt

Output:

00000000: 4f53 204c 6162 0a OS Lab.

๐Ÿงฉ Shows 8 bytes per line instead of 16 — useful for short files.


๐Ÿ“˜ Example 5: Display Only Specific Bytes

Show the first 8 bytes:

xxd -l 8 sample.txt

Skip the first 4 bytes:

xxd -s 4 sample.txt

๐Ÿงฉ 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:

echo "Hello" | xxd

Output:

00000000: 4865 6c6c 6f0a Hello.

๐Ÿงฉ Example 7: View a Binary File

Try this on an executable:

xxd /bin/ls | head

Output (truncated):

00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............ 00000010: 0200 3e00 0100 0000 0000 0000 0000 0000 ..>.............

๐Ÿงฉ Explanation:

  • The first 4 bytes 7f 45 4c 46 correspond 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:

xxd -A sample.txt

๐Ÿงฉ Common Options Summary

OptionDescription
-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:

  1. Create a sample text file:

    echo "Operating Systems Lab" > oslab.txt
  2. Generate hex dump:

    xxd oslab.txt
  3. View plain hex:

    xxd -p oslab.txt
  4. Reverse the hex dump:

    xxd oslab.txt > dump.hex xxd -r dump.hex new.txt cat new.txt
  5. Observe and compare the outputs.


Sample Observation Table

CommandDescriptionSample 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

CommandPurpose
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:

echo "AB" | xxd -g 1

and explain how:

  • 41A

  • 42B
    This helps them visualize ASCII ↔ hex ↔ character mapping

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