Home·Terminal commands

diff

Text processing

Usage

diff -u [old] [new]

Shows what changed between two files; the unified -u form is what patch and code review expect, and it exits 1 when they differ, which stops a script running under set -e.

Common flags

FlagMeaning
-uUnified output, the format patch and code review expect
-rCompare two directory trees
-qOnly say whether the files differ
-wIgnore all whitespace
-iIgnore case
-NTreat a missing file as empty, which patches need

Examples

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

Writes a patch file.

diff -rq dirA dirB

Lists which files differ between two trees.

diff <(sort a.txt) <(sort b.txt)

Compares two files ignoring line order (bash).

These were built to be piped together. Three joined commands are usually shorter than one that does everything.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does diff do?

Shows what changed between two files; the unified -u form is what patch and code review expect, and it exits 1 when they differ, which stops a script running under set -e.

Q. How do I type it?

diff -u [old] [new] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

6 are listed here; the full set is in man diff. This command sits under Text processing.

Related commands

man page: man diff