The xxd command is used as the hex dumper in Linux that changes the text to hexadecimal numbers. It’s a command line utility used to display the contents in a human-readable format, making it easier to understand how the data is structured. The xxd command can be used to convert hexadecimal to binary and hexadecimal to normal text as well.
This article will provide complete information about the xxd command in Linux with the help of the below-mentioned content:
- Install the xxd utility
- How to Use xxd Command in Linux?
- Default Behaviour of the xxd command
- Set the Column Length Using the xxd Command
- Start from the Specific Number of Characters or Lines
- Limit output to the Specific Hexadecimal Length
- Show output in Binary Digits
- Display the Hexadecimal Output in Uppercase
- Hexadecimal to Normal Text (Reverse Operation)
- Group Bytes of Hexadecimal Data
Install the xxd Utility
The xxd tool is already installed in several Linux distributions. If your system does not have it, you can install it in your Linux distribution by running the following commands:
$ 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?
We can use the xxd command to modify the hexadecimal output. In Linux, the xxd command general syntax is given below:
$ xxd <options> <file-name>
The above command explanation is:
- xxd: Shows that the xxd tool is used.
- options: Put the desired xxd command option to format the hexadecimal output.
- file-name: Specify the filename whose data you want to convert to hexadecimal.
The xxd command comes with the following built-in options:
|c||Format the bytes of data per line that set the column length.|
|s||Start from the specified line.|
|l||Limit the output to a specific length.|
|b||Display the file’s content in binary format.|
|u||Display the values in Upper case letters.|
|r||Reverse the hexadecimal data to normal text.|
|g||Group the bytes of hexadecimal data to a specific number.|
Note: This article will use the file named “testfile.txt” for performing the examples whose content is as follows:
Example 1: Default Behaviour of the xxd command
The default behavior of the xxd command is to display 16 bytes of data per line, including blank space and line breaks. For instance, to check the xxd command default output for the “testfile.txt”, use this command:
$ xxd testfile.txt
The xxd command output shows the following details about the file data:
- First Column (Column 1): The row offset in the file.
- Middle Columns (Columns 2-4): File data in hexadecimal (hexdump).
- Last Column (Column 5): Specified file’s original data having 16 hexadecimal bytes of data per line as highlighted where one digit in the last column is every hexadecimal byte.
Example 2: Set the Column Length Using xxd Command
The xxd command is equipped with the “c” or “cols” options which can limit the bytes of data per column. For instance, the below-mentioned command displays “10” bytes of data in the last column for the testfile.txt file:
Note: The column length will be adjusted according to the bytes of data.
$ xxd -c 10 testfile.txt
The 10 bytes of data are hex dumped in every row, adjusting the column length. The default xxd command takes 16 bytes of data for every row, which can have different columns than the above output (10 bytes per row).
Example 3: Start From the Specific Number of Characters or Lines
The xxd command’s “s” option is used to start from a specific line or skip certain lines. For example, the below command skips the 5 lines (every line is by default 16 bytes) and shows the output from the line number “6” (5×16 = skips 80 bytes of data):
Note: You can use “5” or “0x50” hex bytes in the below command.
$ xxd -s 0x50 testfile.txt
The output displays the file content in hexadecimal by skipping the 5 rows (80 bytes of data).
Moreover, we can display the specific number of line data from the end of the file by adding the negative “–” sign with the specified number. For example, to print the last 5 rows of hexadecimal data for the “testfile.txt”, execute this command:
$ xxd -s -0x50 testfile.txt
The output displays the hexadecimal data of the last 5 rows.
Example 4: Limit Output to the Specific Hexadecimal Length
The “l” option of the xxd command allows the users to limit the output to a specific number of lines. For instance, the following command displays only 2 lines of data in the output:
$ xxd -l 0x20 testfile.txt
The output shows only 2 lines of the data.
Example 5: Show output in Binary Digits
The xxd command defaults the file’s content to hexadecimal by default, but we can use the “b” option to dump the file content in “binary” form. For example, the below command displays the testfile.txt data in binary form:
$ xxd -b testfile.txt
The output shows the file data in binary form instead of hexadecimal.
Example 6: Display the Hexadecimal Output in Uppercase
The hexadecimal output is displayed in lowercase letters by default. If you want to print the hexadeci\mnal output in uppercase, use the “u” option of the xxd command with the specified filename, as shown below:
$ xxd -u testfile.txt
The hexadecimal output is displayed in the uppercase letter.
Example 7: Hexadecimal to Normal Text (Reverse Operation)
We can use the “r” operation to convert the hexadecimal file to normal text. Let’s store the testfile.txt file hexadecimal output of the xxd command to the “outputfile.txt” file with the redirect operator using the following command:
Note: We use the cat command to view the file’s content.
$ xxd testfile.txt > outputfile.txt $ cat outputfile.txt
The output shows that the outputfile.txt file’s content is hexadecimal.
To change the content of the hexadecimal file or normal text (reverse operation), use the “r” option of the xxd command with the desired filename as shown below:
$ xxd -r outputfile.txt
The hexadecimal output is converted to normal text as shown in the output.
Example 8: Group Bytes of Hexadecimal Data
A single line shows 16 bytes of data, while a single hexadecimal output column shows 2 bytes of data (4 digits) by default. To group the hexadecimal data, we can use the “g” option of the xxd command. For example, the below command prints the 5 bytes of hexadecimal data (10 hexadecimal digits) in every hexadecimal column:
$ xxd -g 0x05 testfile.txt
5 bytes of data are shown in every hexadecimal data column.
Note: Several packages are used as an alternative to the xxd command to convert the text to hexadecimal (hex dump), such as od and hexdump commands.
We can convert the text to hexadecimal text with the xxd command. The xxd command can modify the hexadecimal output, such as displaying a specific number of columns, limiting the hexadecimal data per line, showing data in binary, group the hexadecimal data. This post has provided a detailed overview of the xxd command in Linux.