Like od and hexdump commands, xxd is yet another tool used in Linux for hex dump creation. In addition, it can perform the reversion operation.
In this tutorial, we learn about the xxd command in Linux with some examples.
The xxd command is a hex dumper. This means it takes a standard input or a file and then converts it to hexadecimal. It can also convert hex dump back to binary form.
xxd [options] [file]
Default - Understanding the output
Let us understand a simple xxd example that takes a file as input and has contents. You can use the below command to view the hex of a file named datafile.txt
By default, xxd output shows three columns. The columns are file offsets, data in hex (or binary), and data as textual representation (printable ASCII value).
The third column may show a dot if the ASCII character is not printable.
In vi / Vim editor, can convert the data to hexa decimal
xxd is not installed by default in many Linux Distributions. You may get xxd command not found error if not installed. Let's look at how to install it in the following Linux distro:
Ubuntu / Debian
sudo apt install xxd
Install vim-common package on Fedora to have xxd:
sudo dnf install vim-common
Use the command specified below to install the xxd library on Arch OS
pacman -S xxd
Examples of the xxd command
In this section let us understand how the xxd command works with a simple example.
In some cases, the user might need to skip a few lines while printing the hex dump of a file. In this case, the xxd command provides a -s option where you can specify the line number.
In the example, you can see that we are printing the output starting from line 4.
xxd -s 0x30 data.txt
Without -s option, xxd starts at the current file position.
Set Column Length
The xxd offers the -c option in order to choose how long the length of the column needs to be according to the user.
For example to limit the column to four, type
xxd -c 4 datafile.txt
Limit Output to a Specific Length
The xxd command has the option to limit the length of the output to be printed. By default, the hex dump output displays all the contents. In the case of large files, this might be difficult to read, hence you can limit the output hex length of the file.
xxd -l 0x30 datafile.txt
From the output, you can see that we limited hex dump to 3 lines.
The xxd command is used to convert the file contents into hex and binary form. The octets in hex are converted to binary numbers i.e. 0’s and 1’s.
To do so, you can use the -b option along with the xxd command.
xxd -b datafile.txt
The output you can see only in binary digits.
You can combine various options and narrow down the output. In this example let us combine -s and -c options along with the xxd command and see the results.
xxd -c 4 -s 0x30 datafile.txt
Hex Upper Case Letters
The hex values are generally printed in lowercase. However, the xxd command also provides an option -u in order to display in uppercase.
xxd -u datafile.txt
xxd also offers to reverse a hex dump back to binary using the -r option.
xxd -r filenameor
xxd -r -p inputhexfile.txt output.bin
Here you can observe that the xxd command reads the contents of the hex dump file, reverts it back to its binary form, and writes that to another file.
To revert by skipping the first character, take input from piped standard out:
echo '6865 6c6f 0a' | xxd -r -p
If you using vi / Vim editor use
:%!xxd -r to change back to binary. Another way around to convert the data to hexa decimal
xxd Command options
Some useful options of xxd:
|-a||Toggle autoskip: A single '*' replaces nul-lines. Default off.|
|-b||Switch to binary digits dump, rather than hexdump. This option writes octets as eight digits "1"s and "0"s instead of a normal hexadecimal dump|
|-c||Format <cols> octets per line. Default 16|
|-h||Print a summary of available commands and exit.|
|-l||Stop after writing len octets.|
|-r||Reverse operation: convert (or patch) hexdump into binary.|
|-s||Skip lines that the user intends to while printing the hexdump of the file|
|-u||Use upper case hex letters. The default is lowercase.|
|-v||Show version string.|
The xxd command is a very useful utility that helps developers understand and work with the data. The reverse feature makes it different from hexdump and od command.
For more details please check out the man page of the hexdump command.
Thanks for reading, please leave your feedback and suggestions in the below comment section.
Navigate all-in-one place of Linux Commands for more learning.