When you are managing massive log files or backing up large databases in Linux, you typically compress the data using gzip to save disk space. If you later need to verify that a backup archive exactly matches the original text file, or you need to compare two different compressed archives to see what changed, you have a problem: standard comparison tools like diff and cmp cannot read compressed binary data. You would normally have to uncompress the gigabytes of data back to disk just to compare them. To solve this, Linux provides the zcmp command, which compares compressed files on the fly without wasting storage space.
How zcmp Works
The zcmp command is simply a wrapper script for the standard cmp utility. When you run zcmp against a .gz file, it temporarily uncompresses the data into your system’s RAM (memory), streams it directly into the cmp tool for byte-by-byte comparison, and then throws the data away. It never writes the uncompressed files to your hard drive.
Comparing Two Compressed Files
To compare two .gz files to see if their internal, uncompressed contents are identical, simply pass both filenames to the command:
zcmp backup_monday.txt.gz backup_tuesday.txt.gz
Because zcmp is based on cmp, it operates silently on success. If the two files are completely identical, the command will instantly return you to the shell prompt without outputting a single word.
If the files are different, zcmp will output a single line detailing the exact location of the first discrepancy it found:
backup_monday.txt.gz backup_tuesday.txt.gz differ: byte 4501, line 32
This tells you immediately that the files are not identical, and the divergence begins on line 32.
Comparing a Compressed File to a Regular File
You do not need two compressed files to use this tool. You can use zcmp to compare a compressed backup archive against a standard, uncompressed text file that is currently sitting on your server.
zcmp backup_monday.txt.gz current_production.txt
The zcmp tool is smart enough to recognize that the second file is not compressed. It will uncompress the first file in memory and compare it directly to the plain text file, returning the exact same byte-and-line mismatch format if differences are found.