Linux super powerful hexadecimal dump tool: XXD command, I will teach you how to use it!

In the Linux operating system, XXD is a hexadecimal dump tool that can convert binary files into hexadecimal representation and display them in a readable form. The XXD command can be used to display file content, edit files, etc. This article will explain how to use the XXD command in Linux.

install xxd command

Usually, the XXD command is already pre-installed in the Linux operating system, so it can be used without installation. If you are not sure whether the XXD command is installed, you can check with the following command:

which xxd

If the path to XXD is returned, the XXD command is already installed. If nothing is returned, you need to install the XXD command first. In Debian/Ubuntu system, you can use the following command to install:

sudo apt-get install xxd

In Red Hat/CentOS system, you can use the following command to install:

sudo yum install vim-common

Use the XXD command to view the file content

The XXD command can be used to view a hexadecimal representation of the file contents. The syntax for using the XXD command is as follows:

xxd <filename>

For example, to view the hexadecimal representation of the file wljslmz.txt, the following command can be used:

xxd wljslmz.txt

After executing this command, the hexadecimal representation of the wljslmz.txt file will be displayed on the terminal as follows:

00000000: 6865 6c6c 6f0a 0a69 0a0a 616d 0a0a 776c  hello..i..am..wl
00000010: 6a73 6c6d 7a0a                           jslmz.

The above output shows the hexadecimal value and corresponding ASCII character of each byte of the wljslmz.txt file. The first column in the output is the offset address in hexadecimal, the second column is hexadecimal, and the third column is ASCII.

Convert the file to hexadecimal representation

The XXD command can also convert a file to a hexadecimal representation and write it to the file. The syntax for converting a file to hexadecimal representation using the XXD command is as follows:

xxd -i <inputfile> <outputfile>

For example, to convert the file wljslmz.txt to a hexadecimal representation and write it to the file wljslmz.c, the following command can be used:

xxd -i wljslmz.txt wljslmz.c

After executing this command, a file named wljslmz.c will be created in the current directory, which contains the hexadecimal representation of the wljslmz.txt file.

In the wljslmz.c file, the following statement can be used to convert the hexadecimal representation back to the original binary data:

unsigned char wljslmz_txt[] = {
  0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a, 0x0a, 0x69, 0x0a, 0x0a, 0x61, 0x6d,
  0x0a, 0x0a, 0x77, 0x6c, 0x6a, 0x73, 0x6c, 0x6d, 0x7a, 0x0a
};
unsigned int wljslmz_txt_len = 22;

edit binaries

The XXD command can also be used to edit binary files. The file wljslmz.bin can be loaded into the vim editor with the following command:

xxd wljslmz.bin | vim -

This command outputs the hexadecimal representation of the file wljslmz.bin to the terminal and passes it to the vim editor for editing. After editing, you can save the file and exit the vim editor with the following command:

:%!xxd -r

This command will convert the hexadecimal representation in the vim editor back to the original binary data and write it to the wljslmz.bin file.

Summarize

This article introduces the basics of using the XXD command in the Linux operating system, including how to install the XXD command, how to use the XXD command to view file content, convert files to hexadecimal representation, and edit binary files. The XXD command is a very useful tool. For developers and system administrators, it is very important to master the use of the XXD command.

Supongo que te gusta

Origin blog.csdn.net/weixin_43025343/article/details/130144422
Recomendado
Clasificación