zdiff
Show line-level differences between compressed files
By CMD Script Team · 4 min read · Last updated
zdiff [OPTIONS] FILE1.gz FILE2[.gz]Options
| Flag | Description |
|---|---|
-q | Brief mode: report only whether the files differ, not the actual differences |
-u | Produce output in unified diff format |
-c | Produce output in context diff format |
-i | Ignore case differences when comparing lines |
-w | Ignore whitespace differences when comparing lines |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
zdiff compares two files — at least one of which is gzip-compressed — by decompressing
them on the fly and passing the result to diff. It shows a full line-by-line diff of
the content, exactly like running diff on the decompressed files would, but without
ever writing an uncompressed copy to disk. Every option diff understands can be passed
through zdiff as well.
Beginner examples
zdiff file1.gz file2.gz— show a line-by-line diff between two compressed fileszdiff file1.gz file2— compare a compressed file against a plain onezdiff -q file1.gz file2.gz— just report whether they differ, no detailszdiff -u file1.gz file2.gz— unified diff format, likediff -u
zdiff app.conf.gz app.conf.new.gz
Advanced examples
- Review exactly what changed between two nightly compressed config snapshots:
zdiff -u yesterday-config.gz today-config.gz - Ignore incidental whitespace churn while reviewing a diff of a compressed export:
zdiff -w old-export.csv.gz new-export.csv.gz - Quickly gate a script on whether two compressed archives changed at all before doing
expensive work:
zdiff -q a.gz b.gz || echo "changed, reprocessing" - Pipe unified-diff output into a patch-aware reviewer or save it for later:
zdiff -u v1.log.gz v2.log.gz > changes.patch
zdiff -u /backups/etc-2026-07-04.tar.gz /backups/etc-2026-07-05.tar.gz | less
Common mistakes
- Expecting
zdiffto compare the raw compressed bytes — it decompresses first, which is almost always what you want, but can surprise someone expecting a binary-level comparison (usecmpon the raw.gzfiles for that instead). - Using
zdiffon huge files and being overwhelmed by output — pair with-qfirst to check if there's even a difference before generating a full diff. - Forgetting
-i/-wexist and manually pre-processing files to strip case or whitespace differences before comparing. - Confusing
zdiff's job withzcmp's — reaching forzdiffwhen you only need a fast yes/no answer wastes time generating a diff nobody reads.
Tips
- Start with
zdiff -qfor a cheap changed/unchanged check, then re-run without-q(or with-u) only if you need to see specifics. - Use
-uwhen you intend to share or archive the diff — unified format is what most tooling (patch, code review tools) expects. - Combine with
-w/-iwhen comparing generated or formatted files where whitespace or case changes are expected noise, not meaningful differences.
Best practices
- Prefer
zdiffover manually decompressing two files into temp copies just to rundiff— it's fewer steps and leaves no cleanup behind. - In config-management or backup-verification scripts, use
zdiff -qfor gating logic and reserve full diff output for human review or logging. - Version-control the human-readable output of important
zdiff -ucomparisons (e.g., during audits) rather than the raw compressed files, since the diff is what's actually reviewed.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Reviewing what changed between two compressed configuration backups before rolling a change forward or back.
- Auditing differences between two versions of a compressed log or data export during root-cause analysis of a data pipeline discrepancy.
- Verifying that a restored, re-compressed backup is functionally identical in content to the original, even if the compressed bytes differ due to a different gzip version.
Common interview questions
- What does zdiff do differently from diff? It transparently decompresses gzip-compressed inputs before comparing, so you never need to manually decompress files just to diff their contents.
- When would you use zcmp instead of zdiff? When you only need to know if two files differ (and where the first difference is), not what the actual line-by-line changes are — zcmp is faster and simpler for that.
- How would you get a unified diff between two compressed log files?
zdiff -u file1.log.gz file2.log.gz.
Frequently Asked Questions
How is zdiff different from zcmp?
zdiff wraps diff and shows a full, human-readable line-by-line diff of the decompressed content. zcmp wraps cmp and only reports the first byte/line offset where files differ — useful for a fast identical/not-identical check, not for seeing what changed.
Do both files need to be compressed for zdiff to work?
No. Only one input needs to be gzip-compressed; zdiff decompresses whichever arguments are compressed and passes the rest through to diff unchanged.
Can I get a unified diff (like git diff) between two compressed configs?
Yes — `zdiff -u old-config.gz new-config.gz` produces standard unified diff output, which is easy to read and can be piped into tools that expect patch format.
Cheat sheet
Download a quick-reference cheat sheet for zdiff.