diff, cmp and patch
Compare files and directories line by line or byte by byte, make a patch and apply it
diff compares two files line by line and prints what to change in the first to get the second. With -r it compares whole directories. Output in -u (unified) format is a "patch": people read it in code review, and the patch command applies it. cmp compares files byte by byte — for binary data and a quick "are they identical" check.
Syntax
diff [options] old new
diff -r [options] old_dir new_dir
cmp [options] file1 file2 [skip1 [skip2]]
patch [options] [file] < changes.patch
patch -p1 < changes.patch # from the project root
Instead of a file you can give - (standard input) or process substitution: diff <(command1) <(command2).
Exit codes
| Code | diff | cmp |
|---|---|---|
0 | files are identical | files are identical |
1 | there are differences | there are differences |
2 | error (no file, no permission) | error |
1 isn't an error but a result. In if diff -q a b >/dev/null; then … the then branch runs when the files are identical.
Output formats
| Option | Format |
|---|---|
| (no options) | "normal": commands like 2c2, 4a5, 3d2 and lines with < (from the first file) and > (from the second) |
-u, -U N | unified: N lines of context around a change (default 3) — the standard for patches and review |
-c, -C N | context: the old format with *** and ---, rarely seen |
-y | two columns side by side: | — line changed, < — only on the left, > — only on the right |
-W N | output width for -y (default 130) |
--suppress-common-lines | in -y, don't show identical lines |
-q | only say whether the files differ, no details |
-s | also report identical files (Files a and b are identical) |
--color=always | color the output (auto — only in a terminal) |
--label name | file name in the -u header instead of the real one (twice — for both) |
Reading a unified diff
--- a 2026-10-13 15:20:41 +0000 ← old file and modification time
+++ b 2026-10-13 15:20:41 +0000 ← new file
@@ -1,4 +1,5 @@ ← hunk: in the old file 4 lines from line 1, in the new one 5 from line 1
one ← space: unchanged line (context)
-two ← minus: line removed
+2 ← plus: line added
three
four
+five
A changed line is a pair "- old, + new". The line \ No newline at end of file means the previous line has no \n at the end. @@ -1 +0,0 @@ means: in the new file there are no lines at this spot (the file was deleted or emptied).
What to ignore when comparing
| Option | What it ignores |
|---|---|
-w | all spaces and tabs: xy and x y are equal |
-b | changes in the amount of whitespace (and trailing whitespace), but not its appearance: x y = x y, but xy ≠ x y |
-Z | only trailing whitespace |
-B | added and removed blank lines |
-i | letter case |
-I regex | changes in which all lines match the regex (e.g. -I '^#' — comments) |
--strip-trailing-cr | \r at the end of lines (files from Windows) |
-a | treat files as text even if they look binary |
Directories
| Option | What it does |
|---|---|
-r | compare directories recursively |
-q | with -r: only the list of differing files and Only in … |
-N | treat a missing file as empty: instead of Only in you get a full diff (needed for a patch that creates and deletes files) |
-x 'pattern' | exclude files by name pattern (can be repeated) |
-X file | read exclude patterns from a file |
cmp: byte-by-byte comparison
| Option | What it does |
|---|---|
| (no options) | the first difference: a b differ: char 2, line 1 (byte number from 1) |
-l | all differing bytes: byte number (decimal) and both values in octal |
-b | show the differing bytes themselves as characters |
-s | print nothing, only the exit code |
-n N | compare at most N bytes |
-i N | skip the first N bytes in both files (-i N:M — different skips) |
If one file is the beginning of the other, cmp prints EOF on a after byte 2 and returns 1.
patch: apply changes
| Option | What it does |
|---|---|
-pN | strip the first N directories from the paths in the patch: for --- a/src/x.c you need -p1 to get src/x.c |
--dry-run | only check whether it applies, change nothing |
-R | revert the patch (apply it in reverse) |
-i file | read the patch from a file instead of standard input |
-b | keep the original as file.orig |
-N | skip hunks that are already applied without asking |
-o file | write the result to a different file |
-d directory | change to the directory before applying |
If the file has shifted a bit since, patch finds the place itself and reports Hunk #1 succeeded at 4 (offset 2 lines). Hunks that don't apply are written to file.rej.
Examples
printf 'one\ntwo\nthree\nfour\n' > a.txt
printf 'one\n2\nthree\nfour\nfive\n' > b.txt
# normal and unified output
diff a.txt b.txt
diff -u a.txt b.txt
# side by side, differences only
diff -y --suppress-common-lines -W 40 a.txt b.txt
# identical or not — in a script
if diff -q a.txt b.txt >/dev/null; then echo same; else echo differ; fi
# compare the output of two commands without temp files
diff <(printf 'a\nb\n') <(printf 'a\nc\n')
# directories: list of differences, ignoring temp files
mkdir -p old new && echo 1 > old/x && echo 2 > new/x && echo n > new/added
diff -rq -x '*.tmp' old new
# make a patch for a directory and apply it to a copy
diff -ruN old new > change.patch
cp -r old work && cd work
patch -p1 --dry-run < ../change.patch # check
patch -p1 < ../change.patch # apply
patch -p1 -R < ../change.patch # revert
cd ..
# a patch for a single file
diff -u a.txt b.txt > ab.patch
patch a.txt < ab.patch
# byte by byte: the first difference and all differences
printf 'hello\n' > h1; printf 'hallo!\n' > h2
cmp h1 h2
cmp -l h1 h2
cmp -s h1 h2 && echo same
Pitfalls
- Exit code 1 under
set -estops the script.diffwith differences is a "failure" to the shell. Writediff a b || trueor check the code in anif. - Argument order matters.
diff old new: lines with-/<come from the first file. Swapped arguments produce a "reversed" patch, andpatchwill offer to apply it with-R(Reversed (or previously applied) patch detected!). - Without
-Nnew and deleted files don't make it into the patch:diff -ronly saysOnly in new: added, andpatchskips such a line. - A wrong
-pgivescan't find file to patch … Perhaps you used the wrong -p. Patches fromgit diffanddiff -ru old neware applied from the project root with-p1. Always run--dry-runfirst. patchasks questions when in doubt (Assume -R? [n],File to patch:). In a terminal it waits for an answer; without a terminal it silently takes the default "no" and skips the hunk. In a script be explicit:-N— skip what's already applied,-R— revert, and check the exit code (1 — not all hunks applied).\rat line ends makes every line different. If the file came from Windows, add--strip-trailing-cror remove the\rbeforehand (tr,sed 's/\r$//').-bis not "ignore all whitespace".xyandx ydiffer for-bbut not for-w.cmp -lprints bytes in octal:2 145 141ise(0x65) vsa(0x61), while the byte number is decimal.diffis for text. On binary files it only saysBinary files a and b differ; for bytes usecmpor compare dumps:diff <(xxd a) <(xxd b).
See also
comm · xxd and od · git · checksums · sort