How to Use the zdiff Command to Compare Compressed Files in Linux

When you are managing a Linux server, you will frequently compress log files and configuration backups using gzip to save disk space. If you suspect that a configuration error was introduced recently, you might need to compare an older, compressed backup file with a newer, compressed backup file to see exactly what changed. Normally, you would have to run the gunzip command on both massive archives to extract them to your hard drive, run the diff command to compare the raw text, and then compress them all over again. To save time and disk space, you can use the zdiff command instead.

How zdiff Works

The zdiff command is a powerful wrapper script for the standard diff utility. When you run zdiff, it temporarily uncompresses the .gz files directly into the system’s active RAM (memory). It streams this uncompressed data straight into the diff tool, outputs the line-by-line comparison to your screen, and then discards the data from memory. It never writes the extracted text to your physical disk.

Comparing Two Compressed Files

To compare two .gz archives, simply execute the command followed by both filenames.

zdiff server_config_v1.txt.gz server_config_v2.txt.gz

The output is identical to the standard diff command. It will display exactly which lines were added, removed, or modified between the two files. If the files are identical, the command will simply return you to the prompt without printing anything.

Comparing a Compressed File with a Standard Text File

One of the most useful features of zdiff is its ability to handle mixed file types automatically. If you want to compare your current, uncompressed production configuration file against last week’s compressed backup, you can do so in a single command:

zdiff server_config_backup.txt.gz /etc/server/config.txt

The zdiff tool will recognize that the second file is already plain text. It will uncompress the first file in memory and immediately run the comparison against the live configuration file on your drive, allowing you to instantly spot the divergent settings.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.