Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux xxd command

Linux xxd command

Linux Command Reference Linux Command Reference

xxd is a command-line tool on Linux that converts a file or data into hexadecimal format for display, similar to a hex viewer.

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

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

Syntax format



xxd [options] [inputfile [outputfile]]

Description of the options parameter:

Common usage

Output a file in hexadecimal format:



xxd filename

This command displays the file filename in hexadecimal form.

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 output per line: By default, xxd outputs 16 bytes per line. This can be changed with the -c option:



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 raw binary data, using the -r option:



xxd -r hexfile

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

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



xxd -p filename

Control the output range: Use the -s option to specify the offset at which to start output, and -l to specify the number of bytes to output:



xxd -s 0x10 -l 0x20 filename

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

Example

** 1. Display file contents in hexadecimal**



xxd filename.txt

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

Example output:



00000000: 4865 6c6c 6f0a                           Hello.

2. Specify the number of bytes output per line



xxd -c 8 filename.txt

Display 8 bytes per line.

Example output:



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

3. Restore hexadecimal 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 pure hexadecimal format (without offset addresses and ASCII)**



xxd -p filename.txt

Output pure hexadecimal data, with all data arranged continuously.

Example output:



48656c6c6f0a

** 6. Generate an array format for C language**



xxd -i filename.txt

This command outputs the file contents 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 hexadecimal letters to uppercase



xxd -u filename.txt

This command outputs hexadecimal letters in uppercase.

Example output:



00000000: 4865 6C6C 6F0A                           Hello.

8. Display only a certain length of data



xxd -l 16 filename.txt

This command displays only the first 16 bytes of data.

** 9. Output data in binary form**



xxd -b filename.txt

This command displays the file contents as binary digits rather than hexadecimal.

Example output:



00000000: 01001000 01100101 01101100 01101100 01101111 00001010  Hello.

Linux Command Reference Linux Command Reference