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 -qr tells you which files differ between two folders

Diff
~/tips/tip-diff-qr-compare-folders

diff -qr /srv/site /mnt/backup/site

Compares two folder trees and lists only the files that are missing or changed.

After copying a folder, restoring a backup or syncing two machines, I want to know if both sides really match. diff can compare whole trees.

diff -qr /srv/site /mnt/backup/site

-r walks through all subfolders and -q keeps it short: instead of showing what changed inside each file, it only says which ones differ. The output has lines like these:

Files old/f.txt and new/f.txt differ
Only in old: gone.txt
Only in new/sub: added.txt

No output and exit status 0 means the trees are identical, which makes it easy to use in a script.

To skip noise like logs or caches, add -x with a pattern. It matches file and folder names, and you can give it more than once:

diff -qr -x '*.log' -x cache /srv/site /mnt/backup/site

diff reads every file that exists on both sides completely, so on a big media folder this takes as long as reading all the data twice. That’s also why it’s trustworthy: it compares content, not sizes or dates. If you only want a quick check of names and sizes, rsync -rn --itemize-changes is faster. And diff doesn’t compare permissions or owners at all, so a restore with wrong ownership still looks identical here.

Further reading: diff(1) manual page, GNU diffutils manual

More Diff tips diff -r -x .git -x '*.log' one two 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 All Diff tips (12) All topics

Link