The Linux xxd
command is a powerful utility that is used to display the contents of a file in hexadecimal format. It is commonly used by programmers and system administrators to view the binary data of a file, or to create hex dumps of files for analysis or debugging.
In this tutorial, we will introduce the xxd
command and show you how to use it to display the contents of a file in hexadecimal format. We will also demonstrate some of the advanced features and options of xxd
that allow you to manipulate and edit the contents of a file.
Basic Usage of xxd
To use the xxd
command, simply type xxd
followed by the name of the file that you want to display in hexadecimal format. For example, to display the contents of the hello.txt
file in hexadecimal format, you would use the following command:
xxd hello.txt
This will display the contents of the hello.txt
file in hexadecimal format, with each line of the output representing 16 bytes of data from the file. The output will be formatted as follows:
00000000: 4865 6c6c 6f20 776f 726c 640a Hello world.
Each line of the output consists of the offset of the first byte on the line, followed by a colon, and then the hexadecimal representation of the bytes in the line. The offset is the number of bytes from the beginning of the file to the first byte on the line.
In addition to the hexadecimal representation of the bytes, xxd
also displays the ASCII representation of the bytes on the right-hand side of the output. This allows you to see the characters that are represented by the hexadecimal data.
In the example above, the first line of the output shows the hexadecimal representation of the bytes 48 65 6c 6c 6f 20 77 6f 72 6c 64 0a
Advanced Usage of xxd
The xxd
command has a number of options that allow you to customize and control the output of the command. For example, the -c
option can be used to specify the number of bytes per line in the output. This allows you to change the default of 16 bytes per line to any other value.
For example, to display the contents of the hello.txt
file with 32 bytes per line, you could use the following command:
xxd -c 32 hello.txt
This would produce the following output:
00000000: 4865 6c6c 6f20 776f 726c 640a Hello world.
00000010:
As you can see, the output now shows 32 bytes per line, with the hexadecimal representation of the bytes on the left and the ASCII representation on the right.
In addition to the -c
option, the xxd
command also has a -p
option that allows you to display the output in plain hexadecimal format, without the offsets and ASCII representation. This can be useful if you want to extract the raw hexadecimal data from a file for further processing or analysis.
For example, to display the contents of the hello.txt
file in plain hexadecimal format, you could use the following command:
xxd -p hello.txt
This would produce the following output:
48656c6c6f20776f726c640a
This output shows the hexadecimal representation of the bytes in the hello.txt
file, without the offsets or ASCII representation.
In addition to displaying the contents of a file in hexadecimal format, the xxd
command can also be used to create or modify the contents of a file. To do this, you can use the -r
option to reverse the hexadecimal dump and recreate the original file from the hexadecimal representation.