>_cmd.script

zcmp

Compare compressed files without decompressing to disk

Compression

By CMD Script Team · 4 min read · Last updated

SYNTAX
zcmp [OPTIONS] FILE1.gz FILE2[.gz]

Options

Command options and flags
FlagDescription
-sSilent: report only the exit status, print nothing (useful in scripts)
-lVerbose: list the byte number and differing values (decimal) for every mismatch
-bPrint differing bytes alongside their offsets in a more compact form
-n NUMCompare 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 identical
  • zcmp file1.gz file2 — compare a compressed file against a plain one
  • zcmp -s file1.gz file2.gz — silent mode, check $? for the result
  • zcmp -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 logrotate didn'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 find to 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 zcmp to show a readable diff of content — it only reports byte/line offsets of differences, not the actual differing text; use zdiff for 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 cmp itself would say the raw .gz bytes differ — zcmp avoids this by decompressing first, so always prefer it over plain cmp on .gz files.
  • Assuming zcmp works on .zip/.bz2/.xz — the classic zcmp from the gzip suite is built for .gz/.Z files specifically.

Tips

  • Use -s inside if conditions or &&/|| chains for a clean silent comparison.
  • Pair zcmp -l with head when 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 zdiff instead.

Best practices

  • Prefer zcmp -s in automation over decompressing both files first and running cmp — it's faster and avoids leaving temporary decompressed files around.
  • When verifying backup integrity across two compressed archives, combine zcmp with checksums (sha256sum on the compressed files) for a fast first-pass check, then use zcmp only if checksums disagree and you need to know why.
  • Don't rely on zcmp alone 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 (via cmp) reports only the first differing byte/line, ideal for a quick identical/not-identical check; zdiff (via diff) 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.