If you have backed up two slightly different versions of a massive SQL database dump or a large source code repository, and both versions have been compressed into .bz2 archives to save space, identifying the exact differences between them can be difficult. You cannot simply use the standard diff command, because diff will only see two binary archives that are completely different. Instead of manually unpacking both gigabyte-sized files just to compare them, you can use the Linux bzdiff command to compare the contents of the archives on the fly.
How the bzdiff Command Works
The bzdiff utility is a convenient shell script wrapper. When you point it at two .bz2 compressed files, it temporarily decompresses both streams into memory and pipes the raw text output directly into the standard cmp or diff utilities.
This approach has two massive advantages: first, it saves you from typing multiple chained commands; second, it requires zero free disk space to hold the uncompressed files, as the decompression happens entirely in memory. The original compressed files are never altered.
Comparing Two Compressed Files
To compare two archives, simply provide both filenames as arguments.
bzdiff database_backup_v1.sql.bz2 database_backup_v2.sql.bz2
If the two files are completely identical (meaning the text data inside them is identical, even if the compression timestamps differ), the command will output nothing and silently return to the prompt.
If the files differ, bzdiff will print standard diff output to the terminal, detailing exactly which lines were added, removed, or changed between version 1 and version 2.
2c2
< INSERT INTO users (name) VALUES ('Admin');
---
> INSERT INTO users (name) VALUES ('SuperAdmin');
Comparing a Compressed File to a Regular File
A highly useful, but often overlooked feature of bzdiff is its ability to compare a compressed file against an uncompressed, plain-text file. If you have an old archive (config_old.txt.bz2) and a new, currently active configuration file (config_new.txt), you can compare them directly.
Simply use the minus sign (-) in place of the second filename. The minus sign tells bzdiff to read from the standard input. You can then pipe your uncompressed file directly into it using the cat command.
cat config_new.txt | bzdiff config_old.txt.bz2 -
The script will decompress the old file in memory and instantly compare its text against the live text streaming from the cat command, providing a seamless diff output without ever touching your hard drive.