Linux xxd Command

Linux 命令大全Linux Command Encyclopedia

xxd is a command-line tool in Linux used to convert files or data into hexadecimal format for display, similar to a hex viewer.

The xxd command can also convert hexadecimal data back into binary files.

The xxd command is commonly used for debugging, file content analysis, and data conversion.

Syntax Format

xxd [options] [inputfile [outputfile]]
  • inputfile: Input file. If not specified, reads from standard input.
  • outputfile: Output file. If not specified, outputs to standard output.

options parameter descriptions:

  • -r: Restore hexadecimal data back to binary (reverse operation).
  • -p: Output in continuous plain hex (without offset addresses and ASCII).
  • -c cols: How many bytes per line, default is 16.
  • -g bytes: How many bytes per group, default is 2.
  • -s offset: Read starting from a certain offset in the file (negative numbers indicate starting from the end of the file).
  • -l length: Only display bytes of the specified length.
  • -u: Convert the output hexadecimal characters to uppercase.
  • -i: Output in C language array format.
  • -b: Convert output to binary format instead of hexadecimal.

Common Usage

Output a file in hexadecimal format:

xxd filename

This command displays the file `filename` in hexadecimal.

Convert standard input to hexadecimal:

echo "Hello World" | xxd

This converts the string "Hello World" to hexadecimal and outputs it.

Specify the number of bytes per line: By default, xxd outputs 16 bytes per line. You can use the -c parameter to change it:

xxd -c 8 filename

The above command displays 8 bytes of data per line.

Convert back to original data: xxd can also convert hexadecimal format back to original binary data using the -r parameter:

xxd -r hexfile

This command restores the hexadecimal data in `hexfile` to a binary file.

Display only hexadecimal (without ASCII): Use the -p option to show only plain hex, without the corresponding ASCII characters:

xxd -p filename

Control the output range: Use the -s parameter to specify starting output from a certain offset in the file, and -l to specify the byte length of output:

xxd -s 0x10 -l 0x20 filename

This command starts at offset 0x10 and outputs 0x20 bytes of data.

Examples

1. Display file content in hexadecimal

xxd filename.txt

This command outputs the hexadecimal representation of the file `filename.txt`, with 16 bytes of data per line by default.

Example output:

00000000: 4865 6c6c 6f0a                           Hello.

2. Specify the number of bytes per line

xxd -c 8 filename.txt

Display 8 bytes per line.

Example output:

00000000: 4865 6c6c 6f0a 0000                      Hello...

3. Restore hexadecimal back to the original file

xxd -r hexfile.txt original.bin

This command restores the hexadecimal data in `hexfile.txt` to the binary file `original.bin`.

4. Display starting from a certain offset in the file

xxd -s 0x10 filename.txt

Display hexadecimal content starting from offset 0x10.

5. Output in plain hex format (without offset addresses and ASCII)

xxd -p filename.txt

Outputs plain hex data, all data arranged continuously.

Example output:

48656c6c6f0a

6. Generate array format in C language

xxd -i filename.txt

This command outputs the file content in the form of a C language array.

Example output:

unsigned char filename_txt[] = {
  0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x0a
};
unsigned int filename_txt_len = 6;

7. Convert output hex letters to uppercase

xxd -u filename.txt

This command outputs hexadecimal letters in uppercase.

Example output:

00000000: 4865 6C6C 6F0A                           Hello.

8. Only display data of a certain length

xxd -l 16 filename.txt

This command only displays the first 16 bytes of data.

9. Output data in binary form

xxd -b filename.txt

This command displays the file content in binary bits instead of hexadecimal.

Example output:

00000000: 01001000 01100101 01101100 01101100 01101111 00001010  Hello.

Linux 命令大全Linux Command Encyclopedia

Other Extensions