← Full reference

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

Codediffcmp
0files are identicalfiles are identical
1there are differencesthere are differences
2error (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

OptionFormat
(no options)"normal": commands like 2c2, 4a5, 3d2 and lines with < (from the first file) and > (from the second)
-u, -U Nunified: N lines of context around a change (default 3) — the standard for patches and review
-c, -C Ncontext: the old format with *** and ---, rarely seen
-ytwo columns side by side: | — line changed, < — only on the left, > — only on the right
-W Noutput width for -y (default 130)
--suppress-common-linesin -y, don't show identical lines
-qonly say whether the files differ, no details
-salso report identical files (Files a and b are identical)
--color=alwayscolor the output (auto — only in a terminal)
--label namefile 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

OptionWhat it ignores
-wall spaces and tabs: xy and x y are equal
-bchanges in the amount of whitespace (and trailing whitespace), but not its appearance: x y = x y, but xy ≠ x y
-Zonly trailing whitespace
-Badded and removed blank lines
-iletter case
-I regexchanges in which all lines match the regex (e.g. -I '^#' — comments)
--strip-trailing-cr\r at the end of lines (files from Windows)
-atreat files as text even if they look binary

Directories

OptionWhat it does
-rcompare directories recursively
-qwith -r: only the list of differing files and Only in …
-Ntreat 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 fileread exclude patterns from a file

cmp: byte-by-byte comparison

OptionWhat it does
(no options)the first difference: a b differ: char 2, line 1 (byte number from 1)
-lall differing bytes: byte number (decimal) and both values in octal
-bshow the differing bytes themselves as characters
-sprint nothing, only the exit code
-n Ncompare at most N bytes
-i Nskip 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

OptionWhat it does
-pNstrip the first N directories from the paths in the patch: for --- a/src/x.c you need -p1 to get src/x.c
--dry-runonly check whether it applies, change nothing
-Rrevert the patch (apply it in reverse)
-i fileread the patch from a file instead of standard input
-bkeep the original as file.orig
-Nskip hunks that are already applied without asking
-o filewrite the result to a different file
-d directorychange 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 -e stops the script. diff with differences is a "failure" to the shell. Write diff a b || true or check the code in an if.
  • Argument order matters. diff old new: lines with -/< come from the first file. Swapped arguments produce a "reversed" patch, and patch will offer to apply it with -R (Reversed (or previously applied) patch detected!).
  • Without -N new and deleted files don't make it into the patch: diff -r only says Only in new: added, and patch skips such a line.
  • A wrong -p gives can't find file to patch … Perhaps you used the wrong -p. Patches from git diff and diff -ru old new are applied from the project root with -p1. Always run --dry-run first.
  • patch asks 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).
  • \r at line ends makes every line different. If the file came from Windows, add --strip-trailing-cr or remove the \r beforehand (tr, sed 's/\r$//').
  • -b is not "ignore all whitespace". xy and x y differ for -b but not for -w.
  • cmp -l prints bytes in octal: 2 145 141 is e (0x65) vs a (0x61), while the byte number is decimal.
  • diff is for text. On binary files it only says Binary files a and b differ; for bytes use cmp or compare dumps: diff <(xxd a) <(xxd b).

See also

comm · xxd and od · git · checksums · sort