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 can ignore Windows line endings with one option

Diff
~/tips/tip-diff-strip-trailing-cr

diff --strip-trailing-cr windows.txt unix.txt

Compares a Windows file with a Unix file without every line showing up as changed.

A file that went through a Windows editor gets a carriage return at the end of every line. Compare it with the original and diff says every single line changed, even though they look the same on screen.

diff --strip-trailing-cr windows.txt unix.txt

--strip-trailing-cr removes a carriage return at the end of each line before comparing. A file with Windows line endings and one with Unix line endings give exit status 1 with plain diff, and 0 with this option.

If you’re not sure whether line endings are the problem, I’d ask file, which tells you: it says “with CRLF line terminators” for a Windows file. cat -A shows them too, as ^M$ at the end of each line.

The option only affects the comparison; the files stay the way they are. To really fix a file, convert it with tr -d '\r', dos2unix or a setting in your editor.

GNU diff has it, and so does the Apple diff that macOS ships now. For git repositories this isn’t the right fix at all; there, the core.autocrlf setting or a .gitattributes file keeps the line endings consistent.

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 if ! diff -q /etc/nginx/nginx.conf /srv/backup/nginx.conf > /dev/null; then All Diff tips (12) All topics

Link