The xxd command in Linux: create and reverse hexadecimal spins

Introduction

The commandxxdis a versatile tool present in almost all Linux distributions that allows you to create hexadecimal file spins and, at the same time, reverse that process to get the original file. Its simple syntax and multiple options make it an indispensable ally for developers, system administrators and anyone who needs to inspect or modify data at the binary level.

What's xxd?

Originally part of the packagevim, xxdfunctions as a converter between binary and hexadecimal representation. You can show the content of a file in hexadecimal format with offset addresses and ASCII representation, or read a hexadecimal tilt and rebuild the binary.

Create a hexadecimal tilt

To generate a basic tilt it is enough to redirect the output ofxxdto a file:

  • xxd archivo > archivo.hex
  • xxd -p archivo > archivo.hex(flat output, only hexadecimal without directions)
  • xxd -c 16 archivo > archivo.hex(defines the number of bytes per line, by default 16)

Useful options when creating spins

  • -l longitud: limits the output to the specified number of bytes.
  • -s offset: starts the tilt from a given displacement (in decimal, octal with 0o or hexadecimal with 0x).
  • -u: uses capital letters for A-F digits, which can improve legibility in certain contexts.

Reverse a hexadecimal tilt

The reverse process is done with the option-r(reverse). Depending on the tilt format, variants are used:

  • xxd -r archivo.hex > archivo(reverse a tilt with addresses and ASCII)
  • xxd -r -p archivo.hex > archivo(reverse a flat tilt, only hexadecimal)

Practical examples

Below are some scenarios wherexxdis particularly useful:

  1. Inspect the ELF header of an executable:xxd -l 64 /bin/lsshows the first 64 bytes, enough to see the magic number and the file type.
  2. Modify a specific byte: create a tilt, edit the hexadecimal value with a text editor and repack it withxxd -r.
  3. Compare two binaries without running them: generate flat spins and usediffto detect differences at byte level.

Tips and good practices

  • It always works on a backup of the original file before applying changes by spins.
  • Usexxd -pwhen you need a pure hexadecimal flow to channel it to other tools likegreporsed.
  • Combinexxdwithodorhexdumpas you prefer offset representation or flat format.

Conclusion

The commandxxdIt is a light but powerful tool that simplifies the creation and reversal of hexadecimal overflows in Linux. Dominating your basic and advanced options will allow you to accurately inspect, depurate and modify binary data, saving time and avoiding errors in low-level tasks.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish