本页内容
- Linux bzcmp、bzdiff、bzmore、bzless 和 bzgrep 命令;问题 1:bzcmp 和 bzdiff 命令是如何工作的?问题 2:bzmore 和 bzless 命令是如何工作的?问题 3:bzgrep 命令是如何工作的?
- 结论
- 问题 1:bzcmp 和 bzdiff 命令是如何工作的?
- 问题 2:bzmore 和 bzless 命令是如何工作的?
- 问题 3:bzgrep 命令是如何工作的?
虽然 bzip2 命令行工具可以压缩文件,但还有许多其他工具可以让你执行一些基本任务——比如比较这类文件——而无需先解压缩。在本文教程中,我们将通过一些易于理解的示例来讲解 bzcmp、bzdiff、bzmore、bzless 和 bzgrep 命令的基本用法。
但在开始之前,值得说明的是,本文中的所有示例均在 Ubuntu 22.04 机器上测试过。
Linux bzcmp、bzdiff、bzmore、bzless 和 bzgrep 命令
我们将以问答(Q&A)的形式来讲解这些命令。
问题 1:bzcmp 和 bzdiff 命令是如何工作的?
顾名思义,这些命令用于比较 .bz2 文件。下面是它们的语法:
bzcmp [ cmp_options ] file1 [ file2 ]
bzdiff [ diff_options ] file1 [ file2 ]
下面是手册页(man page)对它们的说明:
Bzcmp and bzdiff are used to invoke the cmp or the diff program on bzip2 compressed files. All options specified are passed directly to cmp or diff. If only 1 file is specified, then the files compared are file1 and an uncompressed file1.bz2. If two files are specified, then they are uncompressed if necessary and fed to cmp or diff. The exit status from cmp or diff is preserved.
下面是一个 bzcmp 和 bzdiff 的基本示例:
bzcmp file1.bz2 file2.bz2
bzdiff file1.bz2 file2.bz2
下面的截图展示了这些命令所产生的输出:

虽然这些命令本身并没有显式提供任何命令行选项,但你可以使用 cmp 和 diff 命令的选项(因为这些工具在内部会被 bzcmp 和 bzdiff 命令所调用)。
问题 2:bzmore 和 bzless 命令是如何工作的?
就像 more 和 less 一样,bzmore 和 bzless 都是用于 CRT 屏幕显示的过滤器,只不过它们针对的是压缩的 bz2 文件。为了让你有更清晰的认识,下面摘录自 bzmore 手册页中的说明:
Bzmore is a filter which allows examination of compressed or plain text files one screenful at a time on a soft-copy terminal. bzmore works on files compressed with bzip2 and also on uncompressed files. If a file does not exist, bzmore looks for a file of the same name with the addi? tion of a .bz2 suffix. Bzmore normally pauses after each screenful, printing --More-- at the bottom of the screen. If the user then types a carriage return, one more line is displayed. If the user hits a space, another screenful is displayed. Other possibilities are enumerated later.
当然,你也可以使用这些命令来查看压缩文件。下面是一个示例:

问题 3:bzgrep 命令是如何工作的?
正如你现在可能已经猜到的,bzgrep 的工作方式与我们在本文中介绍的其他 bz 系列命令如出一辙。具体来说,它就是 grep,但作用于压缩文件。下面摘录自该命令手册页中的说明:
Bzgrep is used to invoke the grep on bzip2-compressed files. All options specified are passed directly to grep. If no file is speci? fied, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep.
下面是一个 bzgrep 的使用示例:
bzgrep "menu" file1.bz2
下面的截图展示了输出结果:

结论
因此,如果你正在处理压缩文件(.bz2),并且希望执行搜索、比较等基本操作,就可以使用这些命令。本文已经讲解了它们的基本用法。如需了解这些工具的更多信息,请参阅它们的手册页(bzdiff、bzmore 和 bzgrep)。