diff -r: Recursively Compare Two Directories
diff -r compares two directories recursively, reporting files that differ and files that exist in only one tree.
When a build emits a whole directory (docs site, generated SDK), diff -r checks the entire tree against the committed copy in one command.
What it does
diff -r descends into subdirectories, comparing files with the same relative path. It prints "Only in <dir>: <name>" for files present in just one tree and a normal diff for files that differ. Exit status is 1 if any difference or unique file is found.
Common usage
diff -r dist/ expected-dist/
# just list what differs, no per-line output
diff -rq dist/ expected-dist/
# ignore noise directories
diff -r --exclude=.git --exclude=node_modules a/ b/Options
| Flag | What it does |
|---|---|
| -r | Recurse into subdirectories |
| -q | Report only which files differ, not the contents |
| --exclude=<pat> | Skip files matching the shell pattern |
| -N | Treat absent files as empty (show them as fully added/removed) |
| -w | Ignore whitespace differences within files |
In CI
Regenerate a directory into a temp path and run diff -rq generated/ committed/ to fail the build on any drift across the whole tree. Add --exclude for timestamps or caches that legitimately vary between runs.
Common errors in CI
"Only in a: file" lines mean a file exists in one tree but not the other, a real difference that sets exit 1; do not ignore them. Reordered "Only in" noise usually comes from build caches or .DS_Store; exclude them. Symlink differences can surface as content diffs; add --no-dereference on GNU diff if you only care about the link targets.