The xxd command utility in Linux makes the hex dump of the files. The readers might be curious to know about the hex dump. The file contents in ASCII characters or binary in the hex dump are converted to hexadecimal numbers. The hexadecimal numbers consist of 1 to 9 numeric numbers and A to F to alphabetic.
In Linux, you can easily convert the files if they are in machine codes to the hex dump file, which human beings will understand.
This blog will act as a complete guide to xxd dumper in Linux. So, let’s start with the basics of the xxd command.
What is the xxd Command in Linux?
As discussed earlier, the xxd command converts the files into Hexa decimal formats in Linux. The general usage syntax of the xxd command is:
$ xxd [Options] [Infile] [Outfile]
The explanation of the above xxd usage syntax is:
- Use the xxd command
- Different options can be used with the xxd commands (Optional)
- Specify the file which is supposed to be converted in the Hexa decimal
- Specify the file where the results are to be stored
The options which can be used with the xxd command are:
|-a||It is used to toggle the auto skip.|
|-b||It converts the contents into binary numbers instead of hexadecimal.|
|-c||It is used to format the octets in a single line. Default 16 octets are allowed, but they can be set to 256.|
|-C||It is used to capitalize the variable name.|
|-g||It is used to separate the bytes with the white space.|
|-h||It displays all the available commands and options of the xxd command.|
|-r||It is used for the reverse operation.|
|-u||It is used for the upper case hex letters.|
|-v||It is used to display the version of the xxd command.|
Many other options can be used with the xxd command, but the abovementioned options are widely used. To better understand the usage of the xxd command, we will demonstrate some examples.
Example 1: Change the File Contents to Hexa Decimal
We have the file in Linux with the name the “myFile,” and we will convert its contents into the Hexadecimals using the command:
$ xxd myFile
We can see that the ASCII characters of the files have been converted into their Hexadecimal values.
Example 2: Exclude a Few Lines Using the xxd Command
We can skip the lines and convert the file into Hexadecimal after some specified lines. For example, if we want to start the conversion from the second line of the file, we will use the “s” option is used to skip the lines and “0x10” (the hexadecimal representation of 1) is used to mention the lines which are to be skipped. Then we will run the command:
$ xxd -s 0x10 myFile
Example 3: Stop the Conversion to a Specific Line
We can also stop the conversion of the file into the Hexadecimal at some certain line. For example, we want to convert the file into Hexadecimal till line 3. The “l” option is used to limit the conversion, and “0x30” is used to limit the conversion to the third line. Then we will use the command:
$ xxd -l 0x30 myFile
Example 4: Limit the Output to Specific Columns
We have seen in the above examples that the output is displayed in columns. For example, we can change to output of example 1 in the 6 columns only by using the “c” option:
$ xxd -c 4 myFile
The output is displayed in four columns.
Example 5: Convert File in Binary Numbers
In all the above examples, we have converted the files into Hexadecimal numbers. But in this example, we will learn the method of converting the file into binary numbers using the xxd command:
$ xxd -b myFile
The output is shown in binary numbers rather than Hexadecimal numbers using the “b” option of the xxd command.
Example 6: Print the Output in Upper Case Hexadecimals
We can see from the above examples when the file is converted to the Hexadecimal number, the alphabets are in lower case.
To display the Hexadecimal in the Upper case, we will use the “u” option:
$ xxd -u myFile
In the above output, the values of the Hexadecimal are in upper case.
This was all about the xxd Hex dumper in Linux.
In Linux, the xxd command converts the file’s contents into Hexadecimal format. The xxd command can be used with its different options. Moreover, the xxd can deal with the file’s content conversion to binary. This post has briefly explained the xxd Hex dumper guide on Linux.