linux命令之xxd学习

1、xxd命令主要用来查看文件对应的十六进制形式,也可以讲文件对应的十六进制形式输出到一个指定的文件。

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

Usage:
       xxd [options] [infile [outfile]]
    or
       xxd -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]]
Options:
    -a          toggle autoskip: A single '*' replaces nul-lines. Default off.
    -b          binary digit dump (incompatible with -ps,-i,-r). Default hex.
    -C          capitalize variable names in C include file style (-i).
    -c cols     format <cols> octets per line. Default 16 (-i: 12, -ps: 30).
    -E          show characters in EBCDIC. Default ASCII.
    -e          little-endian dump (incompatible with -ps,-i,-r).
    -g bytes    number of octets per group in normal output. Default 2 (-e: 4).
    -h          print this summary.
    -i          output in C include file style.
    -l len      stop after <len> octets.
    -o off      add <off> to the displayed file position.
    -ps         output in postscript plain hexdump style.
    -r          reverse operation: convert (or patch) hexdump into binary.
    -r -s off   revert with <off> added to file positions found in hexdump.
    -d          show offset in decimal instead of hex.
    -s [+][-]seek  start at <seek> bytes abs. (or +: rel.) infile offset.
    -u          use upper case hex letters.
    -v          show version: "xxd 2022-01-14 by Juergen Weigert et al.".

关于此命令经常使用的选项主要有:

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

(8)-c: c语言格式显示文件信息

我们可以使用 man 命令查看xxd命令的详细解释:man xxd 或者 xxd -h

2、xxd命令的简单应用

2.1 显示文件的十六进制形式

$ xxd test.txt
00000000: 7465 7374 0a74 6573 7432 0a74 6573 7432  test.test2.test2
00000010: 0a74 6573 7432 0a74 6573 7432 0a         .test2.test2.

2.2 将十六进制显示的内容输出到制定文件

xxd test.txt test.xxd

xxd命令 原文件 输出到的目标文件

$ cat test.xxd
00000000: 7465 7374 0a74 6573 7432 0a74 6573 7432  test.test2.test2
00000010: 0a74 6573 7432 0a74 6573 7432 0a         .test2.test2.

2.3 以二进制形式显示文件内容 (-b选项的使用)

$ xxd -b test.txt
00000000: 01110100 01100101 01110011 01110100 00001010 01110100  test.t
00000006: 01100101 01110011 01110100 00110010 00001010 01110100  est2.t
0000000c: 01100101 01110011 01110100 00110010 00001010 01110100  est2.t
00000012: 01100101 01110011 01110100 00110010 00001010 01110100  est2.t
00000018: 01100101 01110011 01110100 00110010 00001010           est2.

2.4 指定显示文件的字节数

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

使用 -l参数,限定只显示16 bytes的内容

原文链接:https://blog.csdn.net/zhanghui962623727/article/details/91128852

xxd命令可以为给定的标准输入或者文件做一次十六进制的输出,它也可以将十六进制输出转换为原来的二进制格式,即将任意文件转换为十六进制或二进制形式

如果没有给定输入文件,标准输入就作为输入文件infile。如果infile是一个‘-’ 字符,也从标准输入读入。如果没有给定outfile (或者它的文件名是一个‘-'字符), 结果将输出至标准输出。

-a | -autoskip

   打开/关闭 autoskip: 用一个单独的 '*' 来代替空行。默认关闭。
  • -b | -bits
   转到比特(二进制数字)模式,而不是十六进制模式。在这种模式下,每个字符被表示成八个0/1的数字, 而不是一般的十六进制形式。每一行都以一个用十六进制形式表示的行号,后面是 ascii (或ebcdic)形式开头。命令行选项-r, -p, -i在这个模式下不起作用。
  • -c cols | -cols cols

-c cols | -cols cols每行表示个字符。 默认 16 (-i: 12, -ps: 30, -b: 6)。 最多256。

-g bytes | -groupsize bytes

    每<bytes>个字符(每两个十六进制字符或者八个二进制数字)之间用一个空格隔开。用 -g 0禁止分组。在普通模式中<Bytes>默认是2,在二进制模式中是1。分组并不适用于postscript 或者include style 选项。
  • -h | -help
    显示可用命令概述并且退出。不做其它任何事情。
  • -i | -include
    输出为C语言的包含文件形式。 除非xxd从标准输入读入,不然会输出一个完整的静态数组定义(与输入文件同名)。
  • -l len | -len len
    输出<len>个字符后停止。
  • -p | -ps | -postscript | -plain
  以postscript的连续十六进制转储输出。这也叫做纯十六进制转储。
  • -r | -revert
  逆向操作:把十六进制转储转换(或者打补丁)成二进制形式。如果不输出到标准输出,xxd并不把输出文件截断,而是直接写到输出文件。用 -r -p 来从一个没有行号没有某种列格式的纯十六进制转储读入。附加的空格 和换行可以出现在任何地方。
  • -seek offset
   用在-r之后: 会在 当前 文件的 偏移量 上 增加 <offset>。
  • -s [+][-]seek
   从infile的绝对或者相对偏移量<seek>开始。+表示相对于标准输入当前的位置(如果不是标准输入就没有意义了)。- 表示从文件末尾(如果和+连用:从标准输入当前位置)向前数一些字符,从那个地方开始。如果没有 -s选项,xxd从当前位置开始。
  • -u

用大写字母。默认的是小写字母。

-v | -version

   显示版本字符串。
  • 例:

将文件内容转换为十六进制:
xxd test.txt

使用xxd跳过第n行,想要从第6行开始生成十六进制转储(0x50表示第6行,0x00表示第1行)
xxd -s 0x50 test.txt

将输出限制为特定长度,从第一行(0x00)打印到第5行(0x40)
xxd -l 0x50 test.txt

将文件内容转换为二进制文件:
xxd -b test.txt

设置列长,默认列长度为16,即16个字符,包括空格,将列长度设置为9:现在我们将列长度设置为“9”:
xxd -c 9 test.txt

纯16进制转储,输出保存在hex文件中,并使用cat命令从hex文件中读取输出:
xxd -p test.txt > hex

cat hex

还原hex文件,将纯十六进制转储的反向输出打印为了ASCII格式:
xxd -r -p hex

分组大小字节,将输出分组为多少个八位字节,默认是2个字节,接下来设为8,即8个字节一组,分为2组:
xxd -g 8 test.txt

我们常用命令:

xxd -p test.class >> java.txt 转为16进制

xxd -r -p java.txt >> test.class 还原文件

原文链接:

linux命令之xxd学习_xxd命令_宁静致远2021的博客-CSDN博客
https://blog.csdn.net/Angel_asp/article/details/119253052?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.no_search_link

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值