linux命令之xxd學習

1、xxd命令主要用來查看文件對應的十六進制形式,也可以講文件對應的十六進制形式輸出到一個指定的文件。

使用此命令所支持的特有選項,亦可以以二進制的形式查看文件。

關於此命令經常使用的選項主要有:

(1)-a : 它的作用是自動跳過空白內容,默認是關閉的

(2)-c : 它的後面加上數字表示每行顯示多少字節的十六進制數,默認是16bytes,最大是256bytes

(3)-g : 設定以幾個字節爲一塊,默認爲2bytes

(4)-l : 顯示多少字節的內容

(5)-s : 後面接【+-】和address。“+”表示從地址處開始的內容,“-”表示距末尾address開始的內容

(6)-b:以二進制(0 or 1)的形式查看文件內容

(7)-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.(翻譯功力差,直接上原文)

我們可以使用 man 命令查看xxd命令的詳細解釋:man xxd ,部分信息如下:

XXD(1)                                                                  XXD(1)

NAME
       xxd - make a hexdump or do the reverse.

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

DESCRIPTION
       xxd  creates a hex dump of a given file or standard input.  It can also convert a hex dump back
       to its original binary form.  Like uuencode(1) and uudecode(1) it allows  the  transmission  of
       binary  data  in a 鈥榤ail-safe鈥?ASCII representation, but has the advantage of decoding to stan-
       dard output.  Moreover, it can be used to perform binary file patching.

OPTIONS
       If no infile is given, standard input is read.  If infile is specified as a `-鈥?character, then
       input  is  taken  from  standard  input.   If no outfile is given (or a `-鈥?character is in its
       place), results are sent to standard output.

       Note that a "lazy" parser is used which does not check for more than the first  option  letter,
       unless  the  option  is followed by a parameter.  Spaces between a single option letter and its
       parameter are optional.  Parameters to options can be  specified  in  decimal,  hexadecimal  or
       octal notation.  Thus -c8, -c 8, -c 010 and -cols 8 are all equivalent.

       -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  rela-
              tive  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.

2、xxd命令的簡單應用

2.1 顯示文件的十六進制形式

-bash-3.2$ xxd demon.c
0000000: 2369 6e63 6c75 6465 203c 7374 6469 6f2e  #include <stdio.
0000010: 683e 0a2f 2f74 6869 7320 6973 206d 6169  h>.//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;.}..

2.2 將十六進制顯示的內容輸出到制定文件

xxd demon.c demon.xxd

xxd命令   原文件  輸出到的目標文件

2.3 以二進制形式顯示文件內容   (-b選項的使用)

-bash-3.2$ 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;.}..

2.4 指定顯示文件的字節數

-bash-3.2$ xxd -l 16 demon.c
0000000: 2369 6e63 6c75 6465 203c 7374 6469 6f2e  #include <stdio.

使用 -l參數,限定只顯示16 bytes的內容

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章