xxd - Linux


Overview

xxd is a command-line utility in Unix-like systems that creates a hex dump of a given binary file or standard input. It can also convert a hex dump back to its original binary form. xxd is useful for debugging, analyzing the structure of proprietary files, and validating data integrity scenarios.

Syntax

The basic syntax of xxd is as follows:

xxd [options] [infile [outfile]]
  • infile: Specifies the input file. If omitted, xxd reads from standard input.
  • outfile: Specifies the output file. If omitted, xxd writes to standard output.

Options/Flags

  • -a: Toggle autoskip, a feature that compresses strings of zero output.
  • -b: Output in binary rather than hexadecimal.
  • -E: Show output in EBCDIC rather than ASCII.
  • -c num: Format num bytes per line. Default is 16.
  • -g num: Number of bytes per group in normal output.
  • -i: Output in C include file style. A handy option for embedding binary data into source code.
  • -l len: Stop after len Octets.
  • -o off: Add off to the displayed file offsets.
  • -p: Output in postscript continuous hexdump style.
  • -r: Reverse operation: convert (or patch) hexdump into binary.
  • -s off: Start at off offset in the input/input file.
  • -u: Use uppercase hex letters.
  • -v: Display version information and exit.

Examples

  1. Basic Hex Dump:
    Generate a hex dump of file.bin:

    xxd file.bin
    
  2. Formatted Output:
    Display 20 bytes per line:

    xxd -c 20 file.bin
    
  3. Convert and Reverse:
    Convert binary file.bin to hex, then reverse hex back to binary:

    xxd file.bin > file.hex
    xxd -r file.hex file_reversed.bin
    
  4. Include File Style:
    Output suitable for inclusion in a C program:

    xxd -i file.bin file.c
    

Common Issues

  • Large Files: Handling very large files might make xxd less responsive or consume substantial system resources. Stream processing or splitting the file into chunks can mitigate this.
  • Endianness: Confusion about byte order can lead to misinterpretation of data. Always confirm the byte order when interpreting binary data.

Integration

xxd can be effectively combined with other tools:

  • Comparing Binary Files:
    You can use xxd with diff to compare binary files:

    xxd file1.bin > file1.hex
    xxd file2.bin > file2.hex
    diff file1.hex file2.hex
    
  • Scripting:
    In scripts, xxd can be used to convert binary data before processing:

    echo "$(xxd -p file.bin)" | some-processing-script.sh
    
  • hexdump: Similar to xxd but with fewer formatting options and capabilities.
  • od (octal dump): Another utility to dump files in octal and other formats, more flexible in terms of output formats but less user-friendly for hex.

Visit Vim documentation for xxd or the man page (man xxd) for more details and advanced usage.