Where to start First steps track · 15 stages
Linux Labs

diff and patch — comparing and applying changes

4 min GNU diffutils

Comparing files and applying patches: diff two configs, see what changed, apply a patch.

Cheat sheet

8 commands Printable version
  • diff -u a b readable differences
  • diff -qr dir1 dir2 which files differ
  • diff <(cmd1) <(cmd2) compare the output of two commands
  • diff -u orig new > p.patch make a patch
  • patch orig < p.patch apply the patch
  • patch --dry-run -p1 < git.patch check it applies, without writing
  • patch -R < p.patch revert an applied patch
  • vimdiff a b side by side

Overview

diff shows the differences between files; patch applies such differences. Useful for comparing "before/after" in configs, carrying an edit over, or checking what changed.

bash
diff old.conf new.conf            # line-by-line differences
diff -u old.conf new.conf         # unified format (as used in git/patches)

Options

9

Below are the main flags of diff and patch; the full list is in man diff, man patch.

Option What it does Example
-u --unified Unified format with context — readable and usable by patch. diff -u a b
-r --recursive Compare directories recursively. diff -r dir1 dir2
-p N Strip N leading path components (git patches usually need -p1). patch -p1 < change.patch
--dry-run Check whether the patch would apply, without changing files. patch --dry-run -p1 < change.patch
-q --brief Only report whether files differ, without showing the differences. diff -qr dir1 dir2
-w --ignore-all-space Ignore whitespace differences. diff -w a b
-R --reverse Revert an already-applied patch. dangerous patch -R < change.patch
-i --ignore-case Compare case-insensitively. diff -i a b
-B --ignore-blank-lines Ignore inserted and deleted blank lines. diff -B a b

Nothing found. Try a shorter query.

Recipes

2

Commands

bash
diff -u a b                       # unified diff (readable, for patches)
diff -r dir1 dir2                 # recursive across directories
diff -qr dir1 dir2                # just which files differ (fast)
diff --color=auto -u a b          # with color
diff -w a b                       # ignore whitespace ; -i case ; -B blank lines
diff <(cmd1) <(cmd2)              # compare the OUTPUT of two commands (process substitution)
diff <(sort a) <(sort b)          # compare regardless of line order

# make and apply a patch
diff -u orig.conf new.conf > change.patch
patch orig.conf < change.patch    # apply it
patch -p1 < change.patch          # from a git patch (strip 1 path level)
patch --dry-run -p1 < change.patch  # check it without writing
patch -R < change.patch           # revert it (reverse)
vimdiff a b                       # interactive side-by-side comparison

Examples

bash
# your changes versus the package default
diff -u /etc/svc/conf.dpkg-dist /etc/svc/conf

# what would change before applying (git-style)
git diff > my.patch ; patch --dry-run -p1 < my.patch

# three files (a common ancestor plus two versions)
diff3 mine.txt base.txt theirs.txt

# highlighting differences by word, not by line
wdiff old.txt new.txt | colordiff

Troubleshooting

4

patch: Hunk FAILED / .rej files

The context in the target file has already changed. Check the *.rej files (rejected hunks), apply manually, or get a fresh patch. patch --dry-run checks without writing.

I don't know which -p level to use

git patches usually need patch -p1 (strips a/, b/). If paths don't match, try -p0/-p1. patch will ask for a filename if it can't find one.

Too much noise in the diff (whitespace/CRLF/case)

diff -w (ignore whitespace), -i (case), -B (blank lines). For CRLF, fix line endings (sed -i 's/\r$//', see sed-awk).

Compare command output, not files

diff <(cmd1) <(cmd2) — process substitution (bash). Handy: diff <(ssh h1 'rpm -qa'|sort) <(ssh h2 'rpm -qa'|sort).

See also

  • files-basics — Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, terminal copy-paste.
  • git — The basic workflow, branches, reverting, resolving conflicts — the minimum to get work done.
  • sed-awk — Stream editing (sed) and column-based processing (awk).
Loading…