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 -u gives a compact change list that patch and git understand

Linux
~/tips/tip-linux-diff-u-unified-patch

diff -u nginx.conf.bak nginx.conf

Compares two files and prints only the changes, with a few lines of context, in the format patch tools expect.

When a config file changes and I want to know exactly what, I do not want to read both files. A unified diff shows only the changed lines.

diff -u nginx.conf.bak nginx.conf

The -u option selects unified format. It starts with a two-line header naming both files and their timestamps. Each change then sits in a hunk that begins with a line like @@ -4,6 +4,7 @@. Lines starting with - were removed, lines with + were added, and lines with a space are unchanged context.

The default is three lines of context, and -U 5 changes that to five. Three is the minimum that patch needs to apply a change reliably, so do not go lower when you plan to share the result.

diff -u old.conf new.conf > fix.patch
patch old.conf < fix.patch

The caveat is directories. To compare two folders, add -r, and add -N if you want files that exist on only one side to appear as additions or removals. Without -N they are just reported as being present in one folder. Also, diff exits with 1 when the files differ, which counts as a failure in a script with set -e.

Further reading: GNU diffutils: unified format

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link