xxd

xxd creates a hex dump of a binary file, or converts a hex dump back into binary. It ships with Vim and is therefore present on virtually every Linux system — a useful fallback when hexdump, od, or hexedit are unavailable.

Basic usage

# Create a hex dump of a binary file
xxd $BINARY $HEXDUMP
 
# Reconstitute the binary from the dump
xxd -r $HEXDUMP $BINARY

Useful flags

FlagEffect
-l $NDump only the first $N bytes
-s $OFFSETStart at byte offset $OFFSET
-g 1Group output in single bytes (default is 2-byte groups)
-c 1616 bytes per line (the classic hex-editor layout)
-iOutput as a C include file (array definition)
-bBinary digit dump instead of hex

Practical example

# Inspect the first 256 bytes of a suspicious binary
xxd -l 256 suspicious.bin
 
# Patch a byte at offset 0x40 and write back
xxd -s 0x40 -l 1 suspicious.bin
# ... edit the hex ...
xxd -r -s 0x40 -l 1 patch.hex suspicious.bin

Security relevance

  • Malware triage: Quick header inspection before running strings or disassemblers.
  • Cheat-sheet favorite: xxd is standard on minimal containers and rescue images where heavier tools are missing.
  • File carving: xxd -r can reassemble fragments extracted from memory dumps or packet captures.

Sources