xxd Command in Linux with Examples

Written by: Bobbin Zachariah   |   Last updated: December 18, 2023

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.

xxd Command

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.

Syntax:

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

xxd datafile.txt
Default output of xxd

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

Installation

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 xxd on Ubuntu

Fedora

Install vim-common package on Fedora to have xxd:

sudo dnf install vim-common

Arch Linux

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. 

Skip Lines

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.

 Example

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 the file column to four

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.

Example:

xxd -l 0x30 datafile.txt
limit output length

From the output, you can see that we limited hex dump to 3 lines.

Binary Display

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.

Example:

xxd -b datafile.txt
convert to binary digit

The output you can see only in binary digits.

Combine Options

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.

Example:

xxd -c 4 -s 0x30 datafile.txt
combine multiple options

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.

Example:

xxd -u datafile.txt
hex in upper case

Reverse Operation

xxd also offers to reverse a hex dump back to binary using the -r option.

Syntax:

xxd -r filename
or
xxd -r -p inputhexfile.txt output.bin
xxd reverse example

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
skip the first character when reversing

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

xxd Command options

Some useful options of xxd:

OptionsDescription
-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
-hPrint a summary of available commands and exit. 
-l Stop after writing len octets.
-r Reverse operation: convert (or patch) hexdump into binary. 
-sSkip lines that the user intends to while printing the hexdump of the file
-uUse upper case hex letters. The default is lowercase.
-v Show version string.

Conclusion

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.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment