xxd 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

xxd 命令安装:


-bash/zsh: xxd command not found

# Windows (WSL2)
sudo apt-get update sudo apt-get install xxd

# Debian
apt-get install xxd

# Ubuntu
apt-get install xxd

# Arch Linux
pacman -S xxd

# Kali Linux
apt-get install xxd

# CentOS
yum install xxd

# Fedora
dnf install vim-common-2

# Raspbian
apt-get install xxd

# Dockerfile
dockerfile.run/xxd

xxd 命令补充说明:


xxd 命令创建给定文件或标准输入的十六进制转储。 它还可以将十六进制转储转换回其原始二进制形式。 与 uuencode 和 uudecode 一样,它允许以 邮件安全 ASCII 表示形式传输二进制数据,但具有解码为标准输出的优点。

此外,xxd 命令还可用于执行二进制文件修补。

xxd 命令语法:


xxd -h[elp]
xxd [options] [infile [outfile]]
xxd -r[evert] [options] [infile [outfile]]

xxd 命令选项:


-a | -autoskip
  toggle autoskip: A single '*' replaces nul-lines. Default off.

-b | -bits
  Switch to bits (binary digits) dump, rather than hexdump. This option writes octets as eight digits "1"s and "0"s instead of a normal hexadecimal dump. Each line is preceded by a line number in hexadecimal and followed by an ascii (or ebcdic) representation. The command line switches -r, -p, -i do not work with this mode.

-c cols | -cols cols
  format <cols> octets per line. Default 16 (-i: 12, -ps: 30, -b: 6). Max 256.

-E | -EBCDIC
  Change the character encoding in the righthand column from ASCII to EBCDIC. This does not change the hexadecimal representation. The option is meaningless in combinations with -r, -p or -i.

-g bytes | -groupsize bytes
  separate the output of every <bytes> bytes (two hex characters or eight bit-digits each) by a whitespace. Specify -g 0 to suppress grouping. <Bytes> defaults to 2 in normal mode and 1 in bits mode. Grouping does not apply to postscript or include style.

-h | -help
  print a summary of available commands and exit. No hex dumping is performed.

-i | -include
  output in C include file style. A complete static array definition is written (named after the input file), unless xxd reads from stdin.

-l len | -len len
  stop after writing <len> octets.

-p | -ps | -postscript | -plain
  output in postscript continuous hexdump style. Also known as plain hexdump style.

-r | -revert
  reverse operation: convert (or patch) hexdump into binary. If not writing to stdout, xxd writes into its output file without truncating it. Use the combination -r -p to read plain hexadecimal dumps without line number information and without a particular column layout. Additional Whitespace and line-breaks are allowed anywhere.

-seek offset
  When used after -r: revert with <offset> added to file positions found in hexdump.

-s [+][-]seek
  start at <seek> bytes abs. (or rel.) infile offset. + indicates that the seek is relative to the current stdin file position (meaningless when not reading from stdin). - indicates that the seek should be that many characters from the end of the input (or if combined with +: before the current stdin file position). Without -s option, xxd starts at the current file position.

-u
  use upper case hex letters. Default is lower case.

-v | -version
  show version string.

xxd 命令实例:


xxd 显示文件的十六进制形式:

$ xxd demon.c

0000000: 2369 6e63 6c75 6465 203c 7374 6469 6f2e  #include .//this is mai
0000020: 6e20 6675 6e63 0a69 6e74 206d 6169 6e28  n func.int main(
0000030: 290a 7b0a 2020 2020 696e 7420 6e75 6d20  ).{.    int num 
0000040: 3d31 303b 0a20 2020 2070 7269 6e74 6628  =10;.    printf(
0000050: 226e 756d 6265 7220 6973 2025 645c 6e22  "number is %d\n"
0000060: 2c6e 756d 293b 0a20 2020 2072 6574 7572  ,num);.    retur
0000070: 6e20 303b 0a7d 0a0a                      n 0;.}..

xxd 命令使用 -b 二进制形式显示文件内容:

$ xxd -b demon.c

0000000: 00100011 01101001 01101110 01100011 01101100 01110101  #inclu
0000006: 01100100 01100101 00100000 00111100 01110011 01110100  de <st
000000c: 01100100 01101001 01101111 00101110 01101000 00111110  dio.h>
0000012: 00001010 00101111 00101111 01110100 01101000 01101001  .//thi
0000018: 01110011 00100000 01101001 01110011 00100000 01101101  s is m
000001e: 01100001 01101001 01101110 00100000 01100110 01110101  ain fu
0000024: 01101110 01100011 00001010 01101001 01101110 01110100  nc.int
000002a: 00100000 01101101 01100001 01101001 01101110 00101000   main(
0000030: 00101001 00001010 01111011 00001010 00100000 00100000  ).{.  
0000036: 00100000 00100000 01101001 01101110 01110100 00100000    int 
000003c: 01101110 01110101 01101101 00100000 00111101 00110001  num =1
0000042: 00110000 00111011 00001010 00100000 00100000 00100000  0;.   
0000048: 00100000 01110000 01110010 01101001 01101110 01110100   print
000004e: 01100110 00101000 00100010 01101110 01110101 01101101  f("num
0000054: 01100010 01100101 01110010 00100000 01101001 01110011  ber is
000005a: 00100000 00100101 01100100 01011100 01101110 00100010   %d\n"
0000060: 00101100 01101110 01110101 01101101 00101001 00111011  ,num);
0000066: 00001010 00100000 00100000 00100000 00100000 01110010  .    r
000006c: 01100101 01110100 01110101 01110010 01101110 00100000  eturn 
0000072: 00110000 00111011 00001010 01111101 00001010 00001010  0;.}..

xxd 从二进制文件生成十六进制转储并显示输出:

xxd input_file

xxd 从二进制文件生成十六进制转储并将其保存为文本文件:

xxd input_file output_file

xxd 显示更紧凑的输出,用星号替换连续的零(如果有):

xxd -a input_file

xxd 显示输出,共 10 列,每列 1 个八位组(字节):

xxd -c 10 input_file

xxd 仅显示最大长度为 32 字节的输出:

xxd -l 32 input_file

-------------------
# 使用 -l 参数,限定只显示 16 bytes 的内容
$ xxd -l 16 demon.c

0000000: 2369 6e63 6c75 6465 203c 7374 6469 6f2e  #include <stdio.

xxd 以普通模式显示输出,列之间没有任何间隙:

xxd -p input_file

xxd 将纯文本十六进制转储恢复为二进制,并将其保存为二进制文件:

xxd -r -p input_file output_file

xxd 命令扩展阅读:




xxd 命令评论