工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆

xxd使用手册


测试:
 
    以 I386/RedHat6.2 为测试环境
 
目录:
 
    ★ 命令行参数简介
    ★ xxd使用中的注意事项
    ★ xxd使用举例
       . 从偏移0x10开始显示,也就是缺省情况下的第一行不显示
       . 显示倒数0x30个字节(缺省情况下倒数3行)
       . 显示120字节,连续显示,每行20字节
       . 显示120字节,每行12字节
       . 显示0x6c偏移开始的12个字节
       . 创建一个65537字节大小的文件,除了最后一个字节是'A',其余都是0x00
       . 使用autoskip选项
       . 创建只包含一个'A'字节的文件
       . 复制输入文件到输出文件,且给输出文件前部增加100字节的0x00
       . 二进制文件打补丁
       . Read single characters from a serial line
 
★ 命令行参数简介
 
xxd -h
xxd [options] [infile [outfile]]
xxd -r [options] [infile [outfile]]
 
xxd可以创建给定文件或标准输入的hexdump,也可以还原一个hexdump成以前的二进
制文件。如果没有指定文件或指定文件名为-,则采用标准输入。如果没有指定输出
文件或指定文件名为-,则采用标准输出。
 
-a
    toggle autoskip: A single '*' replaces nul-lines. Default off.
 
-b
    采用2进制显示,而不是默认的16进制
 
xxd -l 32 -b scz.sh
0000000: 00100011 00100001 00100000 00101111 01100010 01101001  #! /bi
0000006: 01101110 00101111 01110011 01101000 00001010 00100011  n/sh.#
000000c: 00001010 00100011 00100000 11001000 10100001 11010110  .# ...
0000012: 11110111 11001001 11101000 10110001 10111000 10111010  ......
0000018: 11000101 00001010 00100011 00100000 01100011 01100001  ..# ca
000001e: 01110100 00100000                                      t 
 
-c cols
    默认16,-i指定时采用12,-p指定时采用30,-b指定时采用6,最大256
    -c8、-c 8、-c 010是等价的,其他选项类似。
 
xxd -c 8 -l 32 -g 1 scz.sh
0000000: 23 21 20 2f 62 69 6e 2f  #! /bin/
0000008: 73 68 0a 23 0a 23 20 c8  sh.#.# .
0000010: a1 d6 f7 c9 e8 b1 b8 ba  ........
0000018: c5 0a 23 20 63 61 74 20  ..# cat 
 
-E
    不用ASCII显示,而采用EBCDIC编码,不影响16进制显示
 
xxd -E -l 32 -g 1 scz.sh
0000000: 23 21 20 2f 62 69 6e 2f 73 68 0a 23 0a 23 20 c8  ......>........H
0000010: a1 d6 f7 c9 e8 b1 b8 ba c5 0a 23 20 63 61 74 20  .O7IY...E..../..
 
-g bytes
    比较下面三种显示方式,缺省bytes为2,指定-b时采用1。
 
xxd -l 32 -g 0 scz.sh
0000000: 2321202f62696e2f73680a230a2320c8  #! /bin/sh.#.# .
0000010: a1d6f7c9e8b1b8bac50a232063617420  ..........# cat 
 
xxd -l 32 -g 1 scz.sh
0000000: 23 21 20 2f 62 69 6e 2f 73 68 0a 23 0a 23 20 c8  #! /bin/sh.#.# .
0000010: a1 d6 f7 c9 e8 b1 b8 ba c5 0a 23 20 63 61 74 20  ..........# cat 
 
xxd -l 32 -g 2 scz.sh
0000000: 2321 202f 6269 6e2f 7368 0a23 0a23 20c8  #! /bin/sh.#.# .
0000010: a1d6 f7c9 e8b1 b8ba c50a 2320 6361 7420  ..........# cat 
 
-h 
    显示帮助信息
 
-i 
    以C语言数组形式显示,如果输入来自stdin,则有所区别
 
unsigned char scz_sh[] = {
  0x23, 0x21, 0x20, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x73, 0x68, 0x0a, 0x23,
  0x0a, 0x23, 0x20, 0xc8, 0xa1, 0xd6, 0xf7, 0xc9, 0xe8, 0xb1, 0xb8, 0xba,
  0xc5, 0x0a, 0x23, 0x20, 0x63, 0x61, 0x74, 0x20
};
unsigned int scz_sh_len = 32;
 
注意区分下面两条命令执行效果的不同
 
xxd -l 12 -i < scz.sh
  0x23, 0x21, 0x20, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x73, 0x68, 0x0a, 0x23
 
xxd -l 12 -i scz.sh
unsigned char scz_sh[] = {
  0x23, 0x21, 0x20, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x73, 0x68, 0x0a, 0x23
};
unsigned int scz_sh_len = 12;
 
-l len
    只显示<len>这么多字节
 
-p
    以连续的16进制表示显示,比较下面的不同显示效果
 
xxd -u -l 32 scz.sh
0000000: 2321 202F 6269 6E2F 7368 0A23 0A23 20C8  #! /bin/sh.#.# .
0000010: A1D6 F7C9 E8B1 B8BA C50A 2320 6361 7420  ..........# cat 
 
xxd -u -l 32 -p scz.sh
2321202F62696E2F73680A230A2320C8A1D6F7C9E8B1B8BAC50A23206361
7420
 
-r
    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
    采用大写16进制字母显示,缺省采用小写16进制字母
 
-v
    显示版本信息
 
★ xxd使用中的注意事项
 
xxd -r has some builtin magic while evaluating line number information.  If the  ouput  file
is  seekable,  then  the  linenumbers at the start of each hexdump line may be out of order,
lines may be missing, or overlapping. In these cases xxd will lseek(2) to the next position.
If  the  output  file  is not seekable, only gaps are allowed, which will be filled by null-
bytes.
 
xxd -r never generates parse errors. Garbage is silently skipped.
 
When editing hexdumps, please note that xxd -r skips everything  on  the  input  line  after
reading enough columns of hexadecimal data (see option -c). This also means, that changes to
the printable  ascii  (or  ebcdic)  columns  are  always  ignored.  Reverting  a  plain  (or
postscript)  style  hexdump with xxd -r -p does not depend on the correct number of columns.
Here an thing that looks like a pair of hex-digits is interpreted.
 
xxd -s +seek may be different from xxd -s seek , as lseek(2) is used to "rewind"  input.   A
'+'  makes a difference if the input source is stdin, and if stdin's file position is not at
the start of the file by the time xxd is started and given its input.  The  following  
examples may help to clarify (or further confuse!)...
 
Rewind  stdin before reading; needed because the `cat' has already read to the end of stdin.
% sh -c 'cat > plain_copy; xxd -s 0 > hex_copy' < file
 
Hexdump from file position 0x480 (=1024+128) onwards.  The `+' sign means "relative  to  the
current position", thus the `128' adds to the 1k where dd left off.
% sh -c 'dd of=plain_snippet bs=1k count=1; xxd -s +128 > hex_snippet' < file
 
Hexdump from file position 0x100 ( = 1024-768) on.
% sh -c 'dd of=plain_snippet bs=1k count=1; xxd -s +-768 > hex_snippet' < file
 
However,  this  is a rare situation and the use of `+' is rarely needed.  the author prefers
to monitor the effect of xxd with strace(1) or truss(1), whenever -s is used.
 
★ xxd使用举例
 
.. 从偏移0x10开始显示,也就是缺省情况下的第一行不显示
  xxd -s 0x10 scz.sh
 
.. 显示倒数0x30个字节(缺省情况下倒数3行)
  xxd -s -0x30 -g 1 scz.sh
 
.. 显示120字节,连续显示,每行20字节
  xxd -l 120 -p -c 20 scz.sh
 
.. 显示120字节,每行12字节
  xxd -l 120 -c 12 -g 1 scz.sh
 
.. 显示0x6c偏移开始的12个字节
  xxd -l 12 -c 12 -g 1 -s 0x6c scz.sh
 
.. 创建一个65537字节大小的文件,除了最后一个字节是'A',其余都是0x00
  echo 10000: 41 | xxd -r > scz.txt
 
  od -A x -t x1 scz.txt
  000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  *
  010000 41
  010001
 
.. 使用autoskip选项
 
  xxd -a scz.txt
  0000000: 0000 0000 0000 0000 0000 0000 0000 0000  ................
  *
  0010000: 41                                       A
 
  注意,缺省情况下autoskip功能是关闭的,但od看样子缺省就是打开的。
 
.. 创建只包含一个'A'字节的文件
  echo '010000: 41' | xxd -r -s -0x10000 > scz_1
 
  注意对比这些效果
  echo '010: 41 42 43 44 45 46' | xxd -r -s 0 > scz_1
 
  echo '010: 41 42 43 44 45 46' | xxd -r -s -0x10 > scz_1
  等价于
  echo '0: 41 42 43 44 45 46' | xxd -r -s 0 > scz_1
 
  echo '010: 41 42 43 44 45 46' | xxd -r -s -0x12 > scz_1
  执行的时候报错,xxd: sorry, cannot seek backwards.
 
  echo '010: 41 42 43 44 45 46' | xxd -r -s 0x10 > scz_1
  等价于
  echo '020: 41 42 43 44 45 46' | xxd -r -s 0 > scz_1
 
.. 复制输入文件到输出文件,且给输出文件前部增加100字节的0x00
  xxd scz_1 | xxd -r -s 100 > scz_2
 
  od -A x -t x1 scz_2
  000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  *
  000060 00 00 00 00 41
  000065
 
.. 二进制文件打补丁
  echo '0: 41 42 43 44 45 46' | xxd -r -s 0 > scz_1
  od -A x -t x1 scz_1
  echo '4: 47 48' | xxd -r - scz_1
  od -A x -t x1 scz_1
 
.. Use xxd as a filter within an editor such as vim(1) to hexdump a region 
  marked between `a' and `z'.
  :'a,'z!xxd
 
.. Use  xxd  as  a  filter  within  an editor such as vim(1) to recover a 
  binary hexdump marked between `a' and `z'.
  :'a,'z!xxd -r
 
.. Use xxd as a filter within an editor such as vim(1) to recover one line
  of a hexdump. Move the cursor over the line and type:
  !!xxd -r
 
.. Read single characters from a serial line
  xxd -c1 < /dev/term/b &
  stty < /dev/term/b -echo -opost -isig -icanon min 1
  echo -n foo > /dev/term/b

« 上一篇 | 下一篇 »

Trackbacks

点击获得Trackback地址,Encode: UTF-8

发表评论

评论内容 (必填):