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 -b ignores changes in the amount of whitespace

Diff
~/tips/tip-diff-b-ignore-space-changes

diff -b old.conf new.conf

Hides differences that are only extra or fewer spaces, so the real changes stand out.

An editor that converts tabs, a trailing space someone removed, a reformatted indent: suddenly diff reports half the file as changed. Most of those changes don’t matter.

diff -b old.conf new.conf

-b treats any run of spaces and tabs as equal to any other run, and ignores whitespace at the end of a line. So a b with two spaces and a b with one space and a trailing one count as the same line. Plain diff calls them different and diff -b finds no difference.

There’s a stronger version, -w, which ignores all whitespace, even where there was none: ab and a b are equal with -w but not with -b. I prefer -b, because a missing space between two words is often a real change.

Two related options help with the rest of the noise: -B ignores changes that only add or remove blank lines, and -Z only ignores trailing whitespace.

Don’t use these when whitespace matters. In YAML, Python and Makefiles, indentation is part of the meaning, and a changed indent that -b hides can be exactly the bug you’re looking for. The same goes for patches: one made with -b may not apply to the original file.

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