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 -Naur makes a patch that also adds and removes files

Diff
~/tips/tip-diff-naur-new-files

diff -Naur original/ changed/ > fix.patch

Creates a patch of a whole folder that includes new and deleted files, not only changed ones.

Making a patch of one file is easy with diff -u. For a whole folder where you also added or removed files, you need two more letters.

diff -Naur original/ changed/ > fix.patch

-r goes through subfolders, -u gives the unified format that patch and git understand, and -a treats every file as text. The important one is -N. Without it, a file that exists on one side only gives an “Only in” line, and the patch can’t recreate it. With -N, diff treats the missing file as empty, so a new file becomes a patch that adds all of its lines, and a deleted file one that removes them.

A patch made with -N turns a copy of the original folder into an exact copy of the changed one. The one made without it fixed the changed file but left out the new and deleted ones.

Make the patch from the parent folder, so both paths have one folder name in front. Then anyone can apply it inside their copy with patch -p1, which strips that first part.

I’d be careful with -a on folders that hold images or other binary files. They end up in the patch as unreadable lines and often can’t be applied. Clean out build output and binaries before you make the patch, or skip them with -x.

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

More Diff tips diff -r -x .git -x '*.log' one two diff -y -W 100 old.conf new.conf 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