>_cmd.script

zdiff

Show line-level differences between compressed files

Compression

By CMD Script Team · 4 min read · Last updated

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

Options

Command options and flags
FlagDescription
-qBrief mode: report only whether the files differ, not the actual differences
-uProduce output in unified diff format
-cProduce output in context diff format
-iIgnore case differences when comparing lines
-wIgnore 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 files
  • zdiff file1.gz file2 — compare a compressed file against a plain one
  • zdiff -q file1.gz file2.gz — just report whether they differ, no details
  • zdiff -u file1.gz file2.gz — unified diff format, like diff -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 zdiff to compare the raw compressed bytes — it decompresses first, which is almost always what you want, but can surprise someone expecting a binary-level comparison (use cmp on the raw .gz files for that instead).
  • Using zdiff on huge files and being overwhelmed by output — pair with -q first to check if there's even a difference before generating a full diff.
  • Forgetting -i/-w exist and manually pre-processing files to strip case or whitespace differences before comparing.
  • Confusing zdiff's job with zcmp's — reaching for zdiff when you only need a fast yes/no answer wastes time generating a diff nobody reads.

Tips

  • Start with zdiff -q for a cheap changed/unchanged check, then re-run without -q (or with -u) only if you need to see specifics.
  • Use -u when you intend to share or archive the diff — unified format is what most tooling (patch, code review tools) expects.
  • Combine with -w/-i when comparing generated or formatted files where whitespace or case changes are expected noise, not meaningful differences.

Best practices

  • Prefer zdiff over manually decompressing two files into temp copies just to run diff — it's fewer steps and leaves no cleanup behind.
  • In config-management or backup-verification scripts, use zdiff -q for gating logic and reserve full diff output for human review or logging.
  • Version-control the human-readable output of important zdiff -u comparisons (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.