xxd Command on Linux with Examples
Introduction
Before we discuss xxd command in Linux, let's understand-What is xxd Command ?
The xxd
command is a powerful utility on Linux used for creating hex dumps, converting hex to binary, and reversing the process. It provides a flexible and efficient way to inspect and manipulate binary files.
On Linux, the text is converted to hexadecimal numbers using the xxd command. It is a command-line tool that displays the contents in an intuitive manner so that the data's structure can be more easily understood. Hexadecimal to binary conversion and hexadecimal to regular text conversion are both possible with the xxd command.
This tutorial introduces the xxd
command and its usage. We will also address a few FAQs on xxd Command on Linux.
Install the xxd Utility
Several Linux distributions already include the xxd utility. By executing the following commands, you can add it to your Linux distribution if your system lacks it:
sudo apt install xxd #To install xxd in Debian-based distros
sudo dnf install vim-common #To install xxd in RHEL-based distros
sudo pacman -S xxd #To install xxd in Arch Linux
How to use xxd Command Explained with Examples?
The xxd command can be used to change the hexadecimal output. The general syntax for the xxd command on Linux is as follows:
xxd <options> <file-name>
The following command is explained:
- xxd: Indicates the use of the xxd tool.
- options: To format the hexadecimal output, provide the desired xxd command option.
- file-name: The filename of the data you want to convert to hexadecimal must be specified.
The following built-in options are available with the xxd command:
Options | Uses |
---|---|
c | Set the column length by formatting the bytes of data per line. |
s | Start with the line that has been specified. |
l | Set a maximum length for the output. |
b | Display the binary format of the file's content. |
u | The values should be displayed in uppercase letters. |
r | Convert the hexadecimal data back into regular text. |
g | Hexadecimal data bytes are grouped together into a specific number. |
Note: This tutorial will use the file "testfile.txt" to execute the following examples:
Example 1: Default Behaviour of the xxd command
The xxd command by default displays 16 bytes of data per line, including white space and line breaks. Use this command, for instance, to examine the "testfile.txt" default output of the xxd command:
xxd testfile.txt
The following information about the file data is displayed in the xxd command output:
- First Column (Column 1): The file's first row offset.
- Middle Columns (Columns 2-4): Save data in hexadecimal (hexdump).
- Last Column (Column 5): The highlighted original data from the specified file has 16 hexadecimal bytes per line, with one digit representing each hexadecimal byte.
Example 2: Set the Column Length Using xxd Command
The "c" or "cols" options of the xxd command allow you to specify a maximum number of bytes for each column. For instance, when running the command below on the testfile.txt file, it shows "10" bytes of data in the last column:
Note: The column length will be changed in accordance with the data bytes.
xxd -c 10 testfile.txt
Every row contains a hex dump of the 10 bytes of data, which changes the column length. The output from the xxd command, which uses a different set of columns than the output seen above (10 bytes per row), requires 16 bytes of data for each row.
Example 3: Begin With the Specified Number of Characters or Lines.
Use the "s" option of the xxd command to start at a certain line or to skip a set number of lines. The command below, for instance, skips the first five lines (each line contains by default 16 bytes) and displays the result starting at line number "6" (5 × 16 = 80 bytes of data are skipped):
Note: The command below accepts "5" or "0x50" hex bytes.
xxd -s 0x50 testfile.txt
By omitting the first 5 rows (a total of 80 bytes of data), the output shows the file's content in hexadecimal.
Adding the negative "-" sign to the supplied number allows us to display a set number of lines of data from the file's end. For instance, run the following command to output the last 5 rows of hexadecimal data from the "testfile.txt" file:
xxd -s -0x50 testfile.txt
The last five rows' hexadecimal data are shown in the output.
Example 4: Restrict Output to the Specific Hexadecimal Length
Users of the xxd command can restrict the output to a certain number of lines by using the "l" argument. For instance, the result of the following command only shows 2 lines of data:
xxd -l 0x20 testfile.txt
Only 2 lines of data are displayed in the output.
Example 5: Show output in Binary Digits
The "b" option of the xxd command can be used to dump the file's content in "binary" format instead of the default hexadecimal format. For instance, the command below shows the data from testfile.txt in binary form:
xxd -b testfile.txt
The file data is displayed in binary form rather than hexadecimal in the output.
Example 6: Show the Hexadecimal Output in Uppercase
By default, the hexadecimal output is written in lowercase. Use the "u" option of the xxd command with the provided filename to print the hexa decimnal output in uppercase, as demonstrated below:
xxd -u testfile.txt
Uppercase letters are used to display the hexadecimal output.
Example 7: Hexadecimal to Regular Text (Reverse Operation)
The "r" operation can be used to change the hexadecimal file to regular text. Let's use the redirect operator and the following command to save the testfile.txt file's hexadecimal output from the xxd command to the outputfile.txt file:
Note: To view the contents of the file, we use the cat command.
xxd testfile.txt > outputfile.txt
cat outputfile.txt
The output reveals that the content of the outputfile.txt file is hexadecimal.
Use the "r" option of the xxd command with the appropriate filename to modify the contents of the hexadecimal file or normal text (reverse operation):
xxd -r outputfile.txt
The output displays the conversion of the hexadecimal output to normal text.
Example 8: Group Bytes of Hexadecimal Data
A single hexadecimal output column by default displays 2 bytes of data (4 digits) as opposed to a single line, which displays 16 bytes of data. We can use the "g" option of the xxd command to group the hexadecimal data. For instance, the command below produces 10 hexadecimal digits in each of the five hexadecimal columns:
xxd -g 0x05 testfile.txt
Each hexadecimal data column contains 5 bytes of data.
FAQs on xxd Command in Linux
How do I install the xxd
command on Linux?
The xxd
command is part of the vim-common
package in most Linux distributions. Ensure that this package is installed by using the package manager specific to your distribution. For example, on Ubuntu, you can use sudo apt-get install vim-common
.
How do I create a hex dump of a file using the xxd
command?
To create a hex dump of a file, run the xxd
command followed by the file name. For example: xxd myfile.txt
will display the hexadecimal representation of the file contents.
Can the xxd
command display ASCII representation alongside the hex dump?
Yes, to display ASCII representation alongside the hex dump, use the -a
or --ascii
flag. For example: xxd -a myfile.txt
.
How can I convert a hex dump back into a binary file with the xxd
command?
To convert a hex dump back into a binary file, provide the -r
or --revert
option followed by the hex dump file and the desired output file name. For example: xxd -r hexdump.txt output.bin
.
Can the xxd
command generate a binary file from a hex representation in a text file?
Yes, you can use the -r
or --revert
option with xxd
by specifying a text file containing the hex dump, followed by the output file name. For example: xxd -r hexdump.txt output.bin
.
How do I limit the number of bytes displayed by the xxd
command?
By default, the xxd
command displays the entire file. However, you can limit it to a specific number of bytes using the -l
or --len
option followed by the desired byte count. For example: xxd -l 16 myfile.txt
will display only the first 16 bytes.
Can I use the xxd
command to edit binary files directly?
Yes, xxd
can be used in conjunction with vi
or vim
text editors for editing binary files. It allows you to modify the hex dump, which is then converted back into the binary format.
Conclusion
Using the xxd command, we may change the text's format to hexadecimal text. The xxd command allows you to change the hexadecimal output in a number of ways, including grouping the hexadecimal data, restricting the amount of hexadecimal data that can fit on a line, and displaying data in binary.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.