Linux bzcmp, bzdiff, bzmore, bzless, and bzgrep Commands Explained with Examples
While the bzip2 command line utility can be used to compress files, there are many other tools that let you perform basic tasks - like comparing such files - without the need to uncompress them. Here, in this tutorial, we will discuss the basics of bzcmp, bzdiff, bzmore, bzless, and bzgrep commands using some easy to understand examples.
But before we do that, it's worth mentioning that all examples in this article have been tested on an Ubuntu 18.04 LTS machine.
Linux bzcmp, bzdiff, bzmore, bzless, and bzgrep commands
We'll discuss each of these commands using Q&A-style of presentation.
Q1. How bzcmp and bzdiff commands work?
As their names suggest, these commands are used for comparing .bz2 files. Following is their syntax:
bzcmp [ cmp_options ] file1 [ file2 ]
bzdiff [ diff_options ] file1 [ file2 ]
And here's what the man page says about them:
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.
Here's a basic example of bzcmp and bzdiff:
bzcmp file1.bz2 file2.bz2
bzdiff file1.bz2 file2.bz2
And the following screenshot shows the output produced by these commands:
Q2. How bzmore and bzless commands work?
Just like more and less, both bzmore and bzless are filters for CRT viewing, but for compressed bz2 files. To give you an even better idea, here's an excerpt about bzmore from its man page:
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.
Of course, you can also use these commands to simply view a compressed file. Here's an example:
Q3. How bzgrep command works?
As you'd have guessed by now, bzgrep also works on the lines of other bz series commands we've explained here. Specifically, it's grep, but for compressed files. Following is an excerpt from the man page of this command:
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
Here's an example usage of bzgrep:
bzgrep "menu" file1.bz2
The following screenshot shows the output:
So if you are dealing with compressed files (.bz2), and want to perform basic operations like searching, comparing, and more, you can use these commands. We've discussed the basics here. For more information about these tools, head to their man pages (here, here, and here).