When dealing with massive log files on a Linux server, they are almost always compressed to save storage space. If you compress a file using the bzip2 format (which typically yields smaller archives than gzip), and you need to compare two different versions of that file to see what changed, you face a dilemma. You cannot use the standard cmp tool because it cannot read compressed binary data. Extracting gigabytes of data just to run a comparison is incredibly inefficient. The optimal solution is to use the bzcmp command.
How bzcmp Works
The bzcmp command is a dedicated wrapper for the standard cmp utility, specifically designed for files ending in .bz2. When you execute this command, it temporarily uncompresses the targeted files directly into your system’s RAM (memory), streams the raw text byte-by-byte into the cmp engine, outputs the result, and immediately discards the data. It never writes the massive uncompressed files back to your physical hard drive.
Comparing Two bzip2 Archives
To check if the internal contents of two compressed archives are completely identical, type the command followed by both filenames.
bzcmp error_log_monday.bz2 error_log_tuesday.bz2
Because it relies on the underlying cmp tool, bzcmp follows the Unix philosophy of silence. If the two files are completely identical down to the last byte, the command will simply return you to the terminal prompt without printing any output.
If the files are different, the command will instantly stop at the very first discrepancy and print a single line of output detailing exactly where the deviation occurred:
error_log_monday.bz2 error_log_tuesday.bz2 differ: byte 14052, line 312
This tells you that the files match perfectly until line 312, at which point the data diverges.
Comparing a Compressed File with a Plain Text File
You can also use bzcmp to verify a newly compressed backup against the original, uncompressed source file that is still sitting on your drive.
bzcmp error_log_backup.bz2 current_error_log.txt
The utility is intelligent enough to realize that the second file is not compressed. It will uncompress the first file in memory and stream it alongside the plain text file, performing an exact byte-for-byte comparison to ensure your backup was created flawlessly.