xxd 命令

xxd 用来对指定的文件或来自标准输入的数据进行十六进制转储,也可以将将十六进制转换为原始的二进制格式,允许以邮件安全的 ASCII 表示传输二进制数据,base64 其实也可以达到相同的目的。也可以用来进行二进制文件 patching

选项

如果没有指定输入文件或输出文件,或者指定为 - ,使用标准输入或标准输出

  • -a | -autoskip 开启自动跳过,以 * 代替空行(nul-lines),默认关闭

  • -b | -bits 切换到二进制位模式,而不是十六进制, -r, -p,-i 下不生效

1
2
3
$ echo -n "hello" | xxd -b

00000000: 01101000 01100101 01101100 01101100 01101111 hello
  • -c cols | -cols cols 输出的每行包含 cols 个字节,默认是 16 个 (-i: 12, -ps: 30, -b: 6).最大 256
1
2
3
4
5
$ echo -n "hello" | xxd -b -c 2

00000000: 01101000 01100101 he
00000002: 01101100 01101100 ll
00000004: 01101111 o
  • -E | -EBCDIC ASCII 表示切换为 EBCDIC(国际)编码 meaningless in combinations with -r, -p or -i

  • -g bytes | -groupsize bytes 每 bytes 个字节,添加一个空格进行分隔分组

1
2
3
4
5
$ echo  "hello" | xxd -g 1
00000000: 68 65 6c 6c 6f 0a hello.

$ echo "hello" | xxd -g 2
00000000: 6865 6c6c 6f0a hello.
  • -p | -ps | -postscript | -plain 以连续连续十六进制样式输出
1
2
$ echo  "hello" | xxd -p
68656c6c6f0a
  • -r | -revert 将十六进制转为二进制。如果不是写到 stdout, xxd 在不截断的情况下将其写入输出文件。使用 -r -p 读取连续的十六进制输出(不包含行号信息,没有特定的列布局)

  • -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.

    跳过多少字节

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
xxd text
00000000: 6865 6c6c 6f0a 4f6e 2054 7565 7364 6179 hello.On Tuesday
00000010: 2c20 7468 6520 4c69 6e75 7820 5379 7374 , the Linux Syst
00000020: 656d 0a41 646d 696e 6973 7472 6174 6f72 em.Administrator
00000030: 2773 2067 726f 7570 206d 6565 7469 6e67 's group meeting
00000040: 2077 696c 6c20 6265 2068 656c 642e 5379 will be held.Sy
00000050: 7374 656d 0a41 646d 696e 6973 7472 6174 stem.Administrat
00000060: 6f72 2041 6c6c 2053 7973 7465 6d20 4164 or All System Ad
00000070: 6d69 6e69 7374 7261 746f 7273 2073 686f ministrators sho
00000080: 756c 6420 6174 7465 6e64 2e20 5379 7374 uld attend. Syst
00000090: 656d 0a41 646d 696e 6973 7472 6174 6f72 em.Administrator
000000a0: 2054 6861 6e6b 2079 6f75 2066 6f72 2079 Thank you for y
000000b0: 6f75 7220 6174 7465 6e64 616e 6365 2e0a our attendance..

# xxd 每行默认 16 个字节,0x30 也就是 48 个字节,刚好是前 3 行
xxd -s 0x30 text
00000030: 2773 2067 726f 7570 206d 6565 7469 6e67 's group meeting
00000040: 2077 696c 6c20 6265 2068 656c 642e 5379 will be held.Sy
00000050: 7374 656d 0a41 646d 696e 6973 7472 6174 stem.Administrat
00000060: 6f72 2041 6c6c 2053 7973 7465 6d20 4164 or All System Ad
00000070: 6d69 6e69 7374 7261 746f 7273 2073 686f ministrators sho
00000080: 756c 6420 6174 7465 6e64 2e20 5379 7374 uld attend. Syst
00000090: 656d 0a41 646d 696e 6973 7472 6174 6f72 em.Administrator
000000a0: 2054 6861 6e6b 2079 6f75 2066 6f72 2079 Thank you for y
000000b0: 6f75 7220 6174 7465 6e64 616e 6365 2e0a our attendance..
  • -l len | -len len 只打印出 len 个字节
1
2
xxd -l 5 text
00000000: 6865 6c6c 6f hello
  • -u 使用大写的十六进制字母,默认是小写的

1
2
# Create a 65537 byte file with all bytes 0x00, except for the last one which is 'A' (hex 0x41).
% echo "010000: 41" | xxd -r > file

参考

XXD