The xxd command is helpful when you need to see the hex of files, including executable files. We will see how you can use it with a text file in the section below. Let’s get started!
What the xxd Linux Command Does
The xxd command is a hex dumper. It creates a hex dump of any file fed to it.
The basic syntax is:
When used, the xxd command will output the binary form of the file, line number, and other human-readable strings.
Examining Files With xxd
The xxd is not the only hex dump available on Linux. However, the good thing with xxd is that it is easy to use. You can create a hex dump with it or revert a hex dump to binary. Let’s see how to use xxd.
Suppose you have your file created, and it contains contents. You can use the following command to hex dump the content:
In our case, we will use a file named “xxdsample.txt”.
The hex output will be:
xxd Limits the Output Hex Length
The xxd has a “-l” flag that limits the hex length to be printed. By default, the hex dump shows all contents. This can easily become unreadable in a case of a large file.
To limit the length, use the command:
We’ve limited the xxd to only create a hex dump for the first five lines in the previous case. The output will be:
What if I want to skip some lines instead? Don’t worry. That is possible as seen in the example below.
Hex Output Skip Lines Using xxd
Depending on your case, you may need to skip some lines when printing the hex dump for your file. Luckily, that’s possible. You can set where you want the conversion to start using the “-s” flag followed by the line number.
For instance, to start from line four in our case, we will add the following command:
In the output below, you will see that the first three lines got skipped:
That’s how you can easily skip a few lines using xxd.
Limit Column Length
The xxd offers the “-c” option, letting you decide how long you want the column to be. The xxd command will display the full column length by default, depending on your file’s content. Add the “-c” flag and the column length if you wish to narrow it down.
To limit our file to four columns:
The column length will be limited, and the extra wording will display in another row. Therefore, you’ve reduced the columns and increased the rows, as shown below:
Display Binary Using xxd
The xxd Linux command converts a file’s contents to hex and binary form. The octets in hex convert to binary 0’s and 1’s.
To convert to binary, use the “-b” flag with the xxd.
For the binary conversion, each line starts with a hexadecimal line number.
Combine xxd Options
We’ve seen some of the common usage examples. However, you can combine them to help narrow down your hex. For instance, to print only four columns and skip the first five lines, you can use the following command:
The output will be:
Use Hex Upper Case Letters With xxd
When printing the hex values, the letters are in lower case. However, you can use the “-u” flag to display in uppercase.
Conclusion
This guide shows how to quickly convert a text file to hex using the xxd command-line utility tool. The examples covered will get you to use the xxd command. In addition, you can open the “xxd –help” to get the man page for the xxd command, and you can keep playing around with the tool to understand it better.