Linux xxd 新手命令教程(附实例)Linux xxd 新手命令教程(附实例)Linux xxd 新手命令教程(附实例)Linux xxd 新手命令教程(附实例)
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

Linux xxd 新手命令教程(附实例)

在此页

  1. Linux xxd 命令的用途是什么?
  2. Q1。如何使用xxd命令?
  3. Q2。如何使用 xxd 跳过几行?
  4. Q3。如何将 xxd 输出限制为特定长度?
  5. Q4。如何设置列长?
  6. Q5。如何让 xxd 产生二进制转储?
  7. 结论

需要一个文本文件的十六进制转储?正在寻找可以执行此操作的命令行实用程序?好吧,你很幸运,因为有一个名为 xxd 的命令可以为你做这件事。在本教程中,我们将使用一些易于理解的示例来讨论 xxd。但在我们这样做之前,值得一提的是,这里的所有示例都已经在 Ubuntu 22.04 LTS 机器上进行了测试。

Linux xxd 命令的用途是什么?

Linux 中的 xxd 命令可让您创建 hexdump 甚至执行相反的操作。以下是它的语法:

xxd [OPTIONS] [file]

以下是工具手册页对其的解释:

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 `mail-safe' ASCII representation, but has the  advantage
of decoding to standard output.  Moreover, it can be used to perform binary file patching.

以下是一些 Q&A 风格的示例,可以让您更好地了解 xxd 的工作原理。

Q1。如何使用xxd命令?

这很简单。例如,假设您有一个文本文件 test.txt,其内容如下所示:

No.|Country|Yes/No
01|India|Y
02|US|Y
03|Australia|Y

04|China|N
05|Russia|Y
06|Japan|Y

07|Singapore|Y
08|South Korea|N
09|Finland|Y
10|Ireland|Y

以下是如何使用 xxd 将文件内容转换为十六进制:

xxd test.txt

这是输出:

00000000: 4e6f 2e7c 436f 756e 7472 797c 5965 732f  No.|Country|Yes/
00000010: 4e6f 0a30 317c 496e 6469 617c 590a 3032  No.01|India|Y.02
00000020: 7c55 537c 590a 3033 7c41 7573 7472 616c  |US|Y.03|Austral
00000030: 6961 7c59 0a0a 3034 7c43 6869 6e61 7c4e  ia|Y..04|China|N
00000040: 0a30 357c 5275 7373 6961 7c59 0a30 367c  .05|Russia|Y.06|
00000050: 4a61 7061 6e7c 590a 0a30 377c 5369 6e67  Japan|Y..07|Sing
00000060: 7061 6f72 657c 590a 3038 7c53 6f75 7468  paore|Y.08|South
00000070: 204b 6f72 6561 7c4e 0a30 397c 4669 6e61   Korea|N.09|Fina
00000080: 6c61 6e64 7c59 0a31 307c 4972 656c 616e  land|Y.10|Irelan
00000090: 647c 590a                                d|Y.

Q2。如何使用 xxd 跳过几行?

假设您不想要完整文件的十六进制转储。相反,您希望该工具从特定行开始转换。然后这可以使用 -s 命令来实现。

例如,如果您希望 xxd 从第 3 行开始生成十六进制转储,那么您可以按照以下方式告诉 xxd 执行此操作:

xxd -s 0x30 test.txt

这是产生的输出:

00000030: 6961 7c59 0a0a 3034 7c43 6869 6e61 7c4e  ia|Y..04|China|N
00000040: 0a30 357c 5275 7373 6961 7c59 0a30 367c  .05|Russia|Y.06|
00000050: 4a61 7061 6e7c 590a 0a30 377c 5369 6e67  Japan|Y..07|Sing
00000060: 7061 6f72 657c 590a 3038 7c53 6f75 7468  paore|Y.08|South
00000070: 204b 6f72 6561 7c4e 0a30 397c 4669 6e61   Korea|N.09|Fina
00000080: 6c61 6e64 7c59 0a31 307c 4972 656c 616e  land|Y.10|Irelan
00000090: 647c 590a                                d|Y.

因此,您可以看到在本例中从第 3 行开始生成十六进制转储。

Q3.如何将 xxd 输出限制为特定长度?

在上一节中,我们讨论了如何使 xxd 从特定点开始转换。但是还有一种方法可以将其转换限制在特定点。这可以使用 -l 命令行选项来完成。

例如,要确保 xxd 只为 test.txt 的前三行创建转储,请按以下方式使用它:

xxd -l 0x30 test.txt

这是它产生的输出:

00000000: 4e6f 2e7c 436f 756e 7472 797c 5965 732f  No.|Country|Yes/
00000010: 4e6f 0a30 317c 496e 6469 617c 590a 3032  No.01|India|Y.02
00000020: 7c55 537c 590a 3033 7c41 7573 7472 616c  |US|Y.03|Austral

Q4.如何设置列长?

如果您希望 xxd 在输出中生成更少或更多的列,请使用 -c 选项并在此处指定列数。下面是使用此选项的示例命令:

xxd -c 5 test.txt

这是输出:

00000000: 4e6f 2e7c 43  No.|C
00000005: 6f75 6e74 72  ountr
0000000a: 797c 5965 73  y|Yes
0000000f: 2f4e 6f0a 30  /No.0
00000014: 317c 496e 64  1|Ind
00000019: 6961 7c59 0a  ia|Y.
0000001e: 3032 7c55 53  02|US
00000023: 7c59 0a30 33  |Y.03
00000028: 7c41 7573 74  |Aust
0000002d: 7261 6c69 61  ralia
00000032: 7c59 0a0a 30  |Y..0
00000037: 347c 4368 69  4|Chi
0000003c: 6e61 7c4e 0a  na|N.
00000041: 3035 7c52 75  05|Ru
00000046: 7373 6961 7c  ssia|
0000004b: 590a 3036 7c  Y.06|
00000050: 4a61 7061 6e  Japan
00000055: 7c59 0a0a 30  |Y..0
0000005a: 377c 5369 6e  7|Sin
0000005f: 6770 616f 72  gpaor
00000064: 657c 590a 30  e|Y.0
00000069: 387c 536f 75  8|Sou
0000006e: 7468 204b 6f  th Ko
00000073: 7265 617c 4e  rea|N
00000078: 0a30 397c 46  .09|F
0000007d: 696e 616c 61  inala
00000082: 6e64 7c59 0a  nd|Y.
00000087: 3130 7c49 72  10|Ir
0000008c: 656c 616e 64  eland
00000091: 7c59 0a       |Y.

所以您可以看到输出中只生成了 5 列。

Q5.如何让 xxd 产生二进制转储?

是的,这也是可能的。 -b 命令行选项使 xxd 以二进制数字生成转储。以下是手册页中关于此工具的内容:

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

命令:

xxd -b test.txt

这是一个例子:

结论

如您所见,如果您处理十六进制和二进制文件内容,则 xxd 命令很有用。在本教程中,我们刚刚讨论了 xxd 提供的一些基本选项。完成这些练习后,请前往工具手册页了解更多信息。

©2015-2025 艾丽卡 support@alaica.com