Linux xxd command explained

Once a program has been compiled, it is not easy to get a peek at the source code or to manipulate its behavior. But there is one thing we can do, which is look at the hexadecimal values inside the binary files. We can also make changes to the data and compile it back to a binary file. This will sometimes reveal information about a file, or allow us to modify its behavior if we can manage to edit the right bit.

A classic example is video game hacks. When playing a game, let’s say your character has a health value of 100. Chances are that you can search the game’s binary files for “100,” and by editing this value to “999,” you could change the health of your character in game. This might take a few tries to find the correct bit, but determined users have tried and succeeded at making such edits in the past. This is just an example and it works more reliably in older, basic games, but it illustrates how hex editors can modify a compiled binary file.

In this tutorial, you will learn how install the xxd hexadecimal editor on all major Linux distros. Then, we will go over command examples on how to use the xxd command on a Linux system.

In this tutorial you will learn:

  • How to install the xxd command on major Linux distros
  • How to create a hex dump of a binary file
  • How to convert a hex dump back to binary file
  • xxd Linux command examples
Linux xxd command explained
Linux xxd command explained
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software xxd
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to Install the xxd Command on Linux

You can use the appropriate command below to install the xxd hexadecimal editor command with your system’s package manager.

To install xxd on Ubuntu, Debian, and Linux Mint:

$ sudo apt update
$ sudo apt install xxd

To install xxd on Fedora, CentOS, AlmaLinux, and Red Hat:

$ sudo dnf install xxd

To install xxd on Arch Linux and Manjaro (will install via Vim package as it is included with it):

$ sudo pacman -S xxd

xxd Linux Command Examples

xxd is a command line hex editor that can create a hex dump of a binary file. It can also convert the dump back to binary form later. This gives you the ability to edit the dump file and then convert it back to see how your changes have affected the program.

Let’s see how to use the xxd command through examples below.

  1. The most basic syntax is to just supply a file name to the xxd command. This will generate a hex dump of the file and will be output to your terminal.

    $ xxd file_name
    Here we are looking at a hex dump of a Linux binary file
    Here we are looking at a hex dump of a Linux binary file

    As you can see above, some of the content is easily readable, and we can glean information from this compiled file.

  2. You can use Bash shell redirection > character if you want to dump this data to a file.
    $ xxd file_name > hex_dump
  3. To convert your hexadecimal data back to binary, use the -r option. Unless you want the output to show in your terminal, make sure you redirect it to a new file.
    $ xxd -r hex_dump > binary_file
  4. Another handy option to use with xxd is the -b option. Using this option will convert the file to binary bits instead of a hexadecimal dump.
    $ xxd -b file_name > binary_dump

Closing Thoughts

In this tutorial, we saw how to install and use the xxd command on a Linux system. xxd is a command line program that creates a hexadecimal dump of a binary file. This allows us to glean information that would otherwise be obscured, and also allows us to change different hex bits before converting the file back to binary. Through lots of trial and error, it is possible to edit a file in the way you desire with xxd.