brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: diff -r -x skips folders and files you don't care about

Diff
~/tips/tip-diff-r-x-exclude-patterns

diff -r -x .git -x '*.log' one two

Leaves out files and folders matching a pattern when comparing two directories recursively.

Comparing two project folders with diff -r is great until it starts reporting every file inside .git. The fix is a single option and I wish I had it in my fingers earlier.

diff -r -x .git -x '*.log' one two

-x PAT (long form --exclude=PAT) skips files and directories whose name matches the pattern. You can repeat it as often as you like. I’d quote the pattern, so the shell doesn’t expand *.log before diff sees it.

Here is a small test. Two folders with a changed src/m.c, a different .git/HEAD, and a notes.log that only exists on one side. Plain diff -r one two printed all three differences: the .git/HEAD change, Only in one: notes.log, and the source file. With the two -x options only the source file was left:

diff -r -x .git -x '*.log' one two
diff -r -x .git -x '*.log' one/src/m.c two/src/m.c
1c1
< x
---
> y

When the list gets long, put the patterns in a file, one per line, and use -X FILE (--exclude-from=FILE). That file is easy to keep next to your notes and reuse.

The exit status is still 1 when something differs, so it keeps working in scripts. One caveat: a pattern matches the name of a file or folder, not a path, so -x src/m.c did not exclude my src/m.c in a test, while -x m.c did. That also means a pattern like m.c skips every file with that name, in any folder.

Further reading: Comparing directories (GNU diffutils manual)

More Diff tips diff -y -W 100 old.conf new.conf diff -Naur original/ changed/ > fix.patch patch -p1 --dry-run < ../fix.patch diff --strip-trailing-cr windows.txt unix.txt if ! diff -q /etc/nginx/nginx.conf /srv/backup/nginx.conf > /dev/null; then All Diff tips (12) All topics

Link