xxd is a command-line tool that is primarily used for creating and analyzing hexadecimal dumps from files. It can also be used to reverse the process and convert a hexadecimal dump back into binary form. In this article, let's uncover the practical applications of the "xxd" command in the Linux ecosystem.
What is a 'hexadecimal dump'?
A hexadecimal dump, often called a hex dump, is a representation of binary data in a human-readable format using hexadecimal notation. Each byte of binary data is displayed as a pair of hexadecimal digits, making it easier to understand the data.
Here's an example of the phrase "How are you" in a hexadecimal dump:
486f 7720 6172 6520 796f 750a How are you.
Here, "48" represents 'H,' "6f" represents 'o,' and so on.
Installing xxd on Linux
While most Linux distributions come with the "xxd" command pre-installed, if you encounter an error when attempting to use it, you may need to install it based on your specific distribution.
Red hat/ Fedora based:
sudo dnf install xxd
Installing xxd in FedoraDebian/Ubuntu based:
sudo apt install xxd
Using xxd command:
Syntax:
xxd [options] [file path]
Options available in xxd command
Here's a table that summarizes some of the most common options of the "xxd" command and their descriptions:
Options
| Description
|
---|
-l
| Limit the number of bytes processed or displayed.
|
---|
-g
| Set the number of bytes per group in the output.
|
---|
-c
| Set the number of bytes per line in the output.
|
---|
-s
| Skip a specified number of bytes before processing.
|
---|
-b
| Generate a binary digit dump
|
---|
-u
| Generate includedincluded uppercase hex-dump
|
---|
-ps
| Output in postscript continuous hex-dump style.
|
---|
-i
| Generate a C-included left file with binary data as an array.
|
---|
-r
| Reverse the process by converting a hexadecimal dump back into binary.
|
---|
-v
| Display the version information of "xxd."
|
---|
Generating hexadecimal dumps
To create a hexadecimal dump of a file, you can use the following command:
xxd [file path]
The binary output on the left side represents a hexadecimal memory address or offset, which indicates the position of each line's data in memory or a binary file, then there are two columns:
- The leftThe right column contains the hexadecimal representation of the binary data.
- The right, column shows the ASCII representation.
Output: xxd hello.txtHere we can see, that the hexadecimal representation for 'H' is 48, for 'e' it is 65, the and so on.
Note: Hexadecimal '0a' is a line feed character and is marking the end of the text file.
Storing hexadecimal dump in a file
We can store the generated hexadecimal dump using the redirection operator '>', type the following command:
xxd [file path] > [output file path]
Storing hexadecimal dump in a fileHere, the hexadecimal dump from standard output is stored in a file named hexdump.txt using the the redirection operator.
Converting hexadecimal dump to the original file
xxd -r [hexadecimal dump file path]
Converting hexadecimal dump to the original fileThe hexadecimal dump was saved in a file called "hexdump.txt," and we used the '-r' option to reverse this process, converting the hex dump back into its original format. The resulting data has been stored in a file named "reversed.txt."
Skipping n bytes from start (-s offset)
The xxd command allows you to specify an offset, effectively skipping a certain number of lines or bytes in the input file when generating a hexadecimal dump.
xxd -s [offset] [file path]
Skipping 5 bytes of 'Hello'
Here, xxd skipped 5 bytes of the word 'Hello' and generated a hexadecimal dump for the remaining text.
Display last n bytes (-s -offset)
The "xxd -s" option allows you to specify an offset or skip a certain number of bytes at the beginning of a file when generating a hexadecimal dump.
xxd -s -[number of bytes] [file path]
Hexdump of last 5 bytes 'ine!.'By using the '-' operator in the offset, the resulting hexadecimal dump is generated for the last 5 bytes of the file.
Display first n bytes (-l)
The "xxd -l" option permits you to limit the number of bytes processed or displayed when generating a hexadecimal dump.
xxd -l [number of bytes] [file path]
Displaying the first 20 bytes We are generating a hex dump of the the first 20 bytes (0x14 is hexadecimal equivalent of 20).
Skipping n output lines
Here's another example where we skip lines in the hexadecimal dump by specifying offsets like 0x10 (which is equivalent to 16 in decimal) and 0x20 (equivalent to 32 in decimal). It's the same as skipping bytes, we are just omitting the display of 16 bytes of data per skipped line.
Skipped 1 output lineNumber of octets (grouped bytes) per group (-g)
The -g option allows us to organize the output by grouping a certain number of bytes.
xxd -g [number of octets] [file path]
Hexdump of 4 bytes per groupHere, we have generated a hexadecimal dump with 4 bytes per group.
Limiting the number of columns (-c)
We can use the -c option to limit the number of columns per line in the output dump.
xxd -c [number of columns] [file path]
Output of 5 columns per lineHere, we are limiting the output to 5 columns per line, you can use 0x05 instead of 5 to get the same output.
Note: Other than using hexadecimal as input for options, you can use other formats like decimal and octal too.
For example: To display the first 20 bytes of the file the commands can be:

Here, the number of bytes is 20 in decimal, 0x14 in hexadecimal, and 024 in octal.
Different dump output styles
Binary digit dump (-b)
Instead of generating a hexadecimal dump, the use of the "-b" option allows us to create a dump that represents the binary digits of the data.
xxd -b [file path]
Binary digit dump of hello.txtThe above binary dump, generated using the -b option, shows that the character 'H' is converted to the binary sequence '01001000', the character 'e' is converted to the binary sequence '01100101', and so on.
Plain hexadecimal dump (-ps)
The "xxd -ps" option is used to generate a hexadecimal postscript-style dump, also known as plain hexdump style. It provides a specific output format where the bytes are displayed continuously without line breaks, suitable for certain applications or requirements.
xxd -ps [file path]
Plain hexadecimal dumpUppercase hexadecimal dump (-u)
The "xxd -u" option in the xxd command is used to specify that the hexadecimal output should use uppercase characters instead of the default lowercase characters.
Hexadecimal dump in uppercaseEmbedding Binary Data in C/C++ Code (xxd -i)
While the primary purpose of xxd is to create hexadecimal dumps, xxd -i takes it a step further by generating a C include file that contains the binary data as an array.
xxd -i [input file] > [output.c]
The generated output.c file includes an array with the binary data from hello.txt. For example, you might get something like this in output.c:
Output: xxd -i hello.txt- unsigned char hello_txt[] is an array of unsigned characters, which represents the binary data of hello.txt.
- The array is initialized with a list of hexadecimal values enclosed within curly braces. Each hexadecimal value represents a byte of data, with the 0x prefix denoting a hexadecimal number. The values correspond to the ASCII codes of the characters in "hello.txt."
- unsigned int hello_txt_len is an additional variable that stores the length of the array. In this case, it's set to 30, indicating that the array contains 30 bytes of data.
Now, you can easily use this array in your C or C++ code, making it convenient for embedding binary data, such as images or other resources, directly into your applications.
C program to utilize the output of xxd -i:
You can use the generated C code to display the text from hello.txt as follows:
C
#include <stdio.h>
// The generated data
unsigned char hello_txt[]
= { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x68,
0x6f, 0x77, 0x20, 0x61, 0x72, 0x65, 0x20, 0x79,
0x6f, 0x75, 0x3f, 0x0a, 0x49, 0x27, 0x6d, 0x20,
0x66, 0x69, 0x6e, 0x65, 0x21, 0x0a };
unsigned int hello_txt_len = 30;
int main()
{
// Print the content of hello.txt
for (unsigned int i = 0; i < hello_txt_len; i++) {
putchar(hello_txt[i]);
}
return 0;
}
OutputHello, how are you?
I'm fine!
This C code includes the generated data array hello_txt and its length hello_txt_len. The program iterates through the array and uses the putchar function to display the data, printing the original content of "hello.txt."
Conclusion
The "xxd" command in Linux is a versatile tool that allows users to work with binary data easily. Whether you need to analyze binary files, edit them with a text editor, or convert between hexadecimal dumps and binary data, "xxd" provides a valuable solution. Understanding and utilizing this command can be a significant asset for anyone dealing with binary data in the Linux environment.
Similar Reads
ls Command in Linux
The ls command is one of the most used commands in the Linux terminal to display the files and directories or path in the terminal. So, using the ls command is a basic skill for navigating the Linux file system, handling files, and managing directories.What is the ls Command in LinuxThe ls command i
10 min read
RPM Command in Linux
The RPM (Red Hat Package Manager) command is a fundamental tool in the world of Linux package management. It is widely used in Red Hat-based distributions like Fedora and CentOS, as well as other RPM-based distributions. The RPM command allows users to install, query, verify, and manage software pac
6 min read
shc Command in Linux
SHC is a Shell Script Compiler (SHC) that directly converts the shell script into executable binaries. The main purpose of the shc is to protect the shell script against accidental changes, hide source code, and prevent future modifications. shc takes the shell script specified in the command line f
5 min read
du Command in LINUX
The du command in Linux is a powerful utility that allows users to analyze and report on disk usage within directories and files. Whether youâre trying to identify space-hogging directories, manage disk space efficiently, or simply gain insights into storage consumption, the du command provides valu
4 min read
Unzip Command in Linux
As an open-source operating system, Linux presents plenty of powerful and versatile instructions for dealing with files and directories. One such command that performs an important role in coping with compressed files is the "unzip" command.Compressed files are a common way to keep space and share d
8 min read
dnf Command in Linux
Linux is a versatile operating system. It has many commands that can change the Linux working functionality of the operating system. Every Linux has a different package manager that helps us to install the software in the Linux operating system. The `dnf` command is a package manager command in Red-
4 min read
grep command in Unix/Linux
The grep command in Unix/Linux is a powerful tool used for searching and manipulating text patterns within files. Its name is derived from the ed (editor) command g/re/p (globally search for a regular expression and print matching lines), which reflects its core functionality. grep is widely used by
7 min read
Fun Commands in Linux
Linux isn't just for coding and administrationâit can also be a lot of fun. With a variety of terminal commands, you can add some entertainment to your Linux experience. Below is a list of some cool and fun commands you can use in Linux to enhance your terminal experience. Weâll also cover how to in
3 min read
SED command in Linux | Set 2
We have discussed some of the SED command options in Sed Command in Linux/Unix with examples SED is used for finding, filtering, text substitution, replacement and text manipulations like insertion, deletion search, etc. It's a one of the powerful utilities offered by Linux/Unix systems. We can use
8 min read
Gzip Command in Linux
The gzip command in Linux is a vital tool for compressing files and reducing their sizes. It employs the DEFLATE compression algorithm , which makes it highly effective for compressing large files or preparing data for transfer over networks.Whether youâre a Linux system administrator or a beginner,
5 min read