zcmp
Compare compressed files without decompressing to disk
By CMD Script Team · 4 min read · Last updated
zcmp [OPTIONS] FILE1.gz FILE2[.gz]Options
| Flag | Description |
|---|---|
-s | Silent: report only the exit status, print nothing (useful in scripts) |
-l | Verbose: list the byte number and differing values (decimal) for every mismatch |
-b | Print differing bytes alongside their offsets in a more compact form |
-n NUM | Compare at most NUM bytes from each file |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
zcmp compares two files, at least one of which is gzip-compressed, by decompressing
them on the fly and passing the result to cmp. It reports whether the (decompressed)
contents are identical, and if not, the byte and line number of the first difference —
without ever writing an uncompressed copy to disk. It's a thin wrapper: all the options
you'd pass to cmp work with zcmp too.
Beginner examples
zcmp file1.gz file2.gz— report the first byte/line where the two differ, or silence if they're identicalzcmp file1.gz file2— compare a compressed file against a plain onezcmp -s file1.gz file2.gz— silent mode, check$?for the resultzcmp -l file1.gz file2.gz— list every differing byte, not just the first
zcmp config-old.json.gz config-new.json.gz
Advanced examples
- Use in a script to skip reprocessing unchanged compressed exports:
if zcmp -s yesterday.csv.gz today.csv.gz; then echo "no changes"; fi - Compare two rotated logs to confirm
logrotatedidn't duplicate data:zcmp access.log.1.gz access.log.2.gz - Diagnose exactly where two nightly database dumps diverge, byte by byte:
zcmp -l dump-mon.sql.gz dump-tue.sql.gz | head -20 - Chain with
findto compare a whole batch of paired archives for a migration verification step.
for f in backups/*.gz; do zcmp -s "$f" "mirror/$f" || echo "DIFFERS: $f"; done
Common mistakes
- Expecting
zcmpto show a readable diff of content — it only reports byte/line offsets of differences, not the actual differing text; usezdifffor that. - Forgetting
zcmp's exit status: 0 means identical, 1 means they differ, 2+ means an error (e.g., file not found) — treating "differ" and "error" the same in a script can hide real problems. - Comparing files with different compression settings and being confused why
cmpitself would say the raw.gzbytes differ —zcmpavoids this by decompressing first, so always prefer it over plaincmpon.gzfiles. - Assuming
zcmpworks on.zip/.bz2/.xz— the classiczcmpfrom the gzip suite is built for.gz/.Zfiles specifically.
Tips
- Use
-sinsideifconditions or&&/||chains for a clean silent comparison. - Pair
zcmp -lwithheadwhen comparing large files — the byte-by-byte listing can be enormous if the files diverge early. - For a full content diff rather than a byte-offset report, reach for
zdiffinstead.
Best practices
- Prefer
zcmp -sin automation over decompressing both files first and runningcmp— it's faster and avoids leaving temporary decompressed files around. - When verifying backup integrity across two compressed archives, combine
zcmpwith checksums (sha256sumon the compressed files) for a fast first-pass check, then usezcmponly if checksums disagree and you need to know why. - Don't rely on
zcmpalone to validate archive structure (e.g., valid tar headers) — it only compares byte streams, not semantic archive contents.
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
- Verifying that a freshly restored backup matches the original compressed backup byte-for-byte after a disaster-recovery drill.
- Detecting whether two nightly compressed exports actually changed, to skip unnecessary downstream processing in a data pipeline.
- Confirming that a file transferred over an unreliable link wasn't corrupted, by comparing it to a known-good compressed copy.
Common interview questions
- What does zcmp actually do under the hood? It decompresses the input file(s) and
pipes the result to
cmp, so you get a byte/line-level comparison without manual decompression. - How is zcmp different from zdiff?
zcmp(viacmp) reports only the first differing byte/line, ideal for a quick identical/not-identical check;zdiff(viadiff) shows a full line-by-line diff of the content. - How would you use zcmp in a shell script to skip work when two compressed files are
identical?
zcmp -s file1.gz file2.gz && echo "no change"— check the exit status rather than parsing output.
Frequently Asked Questions
What's the difference between zcmp and zdiff?
zcmp wraps cmp, which reports only the first byte/line offset where two files differ (or that they're identical) — good for a fast yes/no check. zdiff wraps diff and shows a full line-by-line diff of the content, which is more useful when you need to see exactly what changed.
Do I need to decompress files before comparing them?
No — that's the whole point of zcmp. It decompresses both inputs on the fly (in memory/pipes) and feeds them to cmp, so you never write a decompressed copy to disk.
Can I compare a compressed file to an uncompressed one?
Yes. zcmp only requires the first argument to be compressed; the second file can be plain, and if it's also compressed, zcmp decompresses it too.
How do I use zcmp in a script to check if two archives are identical?
Use `zcmp -s a.gz b.gz; echo $?` — an exit status of 0 means identical, 1 means they differ, and anything else means an error occurred (e.g., a missing file).
Cheat sheet
Download a quick-reference cheat sheet for zcmp.