Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

diff Command Guide for Comparing Files and Directories Line by Line

diff shows where two files (or two directory trees) differ, line by line, and is the foundation of patching, version control, and code review.


What is diff, and Why Use It?

diff compares two files line by line and prints each difference as a hunk: lines prefixed with < exist only in the first file, and lines prefixed with > exist only in the second. It also compares whole directory trees recursively, listing which files were added, removed, or changed.

diff is the ancestor of Git’s comparison engine: every git diff you have ever run is diff with better formatting.

Why Learn diff?

  • You changed a config and want to see exactly what moved.
  • You received a patch or a .diff file and need to understand it.
  • Two directories have drifted and you need to know what differs.
  • Scripts depend on it: diff -q is the classic “are these the same?” check in automation.

If two files produce no output from diff, they are byte-for-byte… well, line-for-line identical. Silence is the answer — and the exit code (0 = same, 1 = differ) is what scripts test.


1. Compare Two Files

diff file1.txt file2.txt
  • Prints hunks of the differing lines:
3,4c3,4
< Old heading
< Another old line
---
> New heading
> Another new line
  • The header 3,4c3,4 means: lines 3–4 of file1 changed into lines 3–4 of file2. The --- separates the two versions.

2. Compare Silently (Exit Code Only)

diff -q file1.txt file2.txt
  • Prints nothing if identical; prints Files file1.txt and file2.txt differ otherwise.
  • The exit code (0 = identical, 1 = different) is what scripts should test — this is the cheapest possible comparison.

3. Compare in Unified Format (The One Everyone Recognises)

diff -u file1.txt file2.txt
  • Produces a unified diff: a few lines of context around each change, with - and + markers — exactly the format Git patches use.
  • This is the format you should default to for anything that a human (or a tool like patch) reads.

Example:

diff -u defaults.conf custom.conf
--- defaults.conf   2026-11-30 10:00:00
+++ custom.conf     2026-12-03 16:20:00
@@ -1,4 +1,4 @@
-timeout = 30
+timeout = 90
 retries = 3
-mode = strict
+mode = relaxed

4. Compare in Context Format

diff -c file1.txt file2.txt
  • Older “context diff” format: larger blocks with ! (changed), - (only in first) and + (only in second) markers.
  • Rarely used today; you’ll meet it in old archives. Prefer -u.

5. Show Only the Changes, Ignoring Surroundings

diff -y --suppress-common-lines file1.txt file2.txt
  • Side-by-side view (-y) with the identical lines removed.
  • Add --width=120 for readable output in a terminal.

Example:

diff -y --suppress-common-lines nginx.conf.old nginx.conf --width=140

6. Compare Two Directories Recursively

diff -r dir1/ dir2/
  • Walks both trees and reports, for each file: whether it exists in only one tree, or differs.
  • diff -qr shortens the output to one line per difference — the fastest way to find what drifted between two config backups.

Example:

diff -qr /etc/nginx /etc/nginx.bak

7. Ignore the Noise

diff -u --ignore-all-space file1.txt file2.txt
  • Common filters:
FlagIgnores
-wAll white space
-bChanges in amount of white space
-iCase differences (text)
-BBlank lines
-E, -FTrailing/end whitespace and CR line endings (CRLF vs LF)
  • diff -u -w answers “did anything real change?” even after an editor reformatted the file.

8. Compare Only Which Files Differ (Names Only)

diff -rq dir1 dir2 | grep '^Files' | cut -d' ' -f2
  • Extracts the list of differing file names — a common building block for sync scripts.
  • rsync --dry-run (rsync) does the same job with more control if you’re about to sync.

9. Generate a Patch to Apply Later

diff -u old-version/config.conf new-version/config.conf > my-changes.patch
  • Saves the unified diff as a patch file.
  • Apply it with patch old-version/config.conf < my-changes.patch, or git apply my-changes.patch inside a repo.
  • Generating diffs between two directory trees the same way (diff -ruN dir1 dir2 > changes.patch) is how source tarballs ship source updates.

10. Compare Files With Different Names or Order

diff <(sort file1.txt) <(sort file2.txt)
  • Compares the sorted contents — ignores order entirely, useful for comparing logs where line order isn’t guaranteed.
  • Process substitution (<(...)) makes any pair of commands diffable.

Example:

diff <(cut -d: -f1 /etc/passwd | sort) <(cut -d: -f1 /etc/group | sort)

11. See What diff’s Exit Code Means

diff -q a.txt b.txt
echo $?
  • 0 — the files are identical.
  • 1 — the files differ.
  • 2 — trouble (missing file, unreadable).
  • Scripts rely on this: if diff -q a b >/dev/null; then ...

Common Pitfalls

  • Silence doesn’t mean “nothing to see” — it means “identical”. Run echo $? if unsure; diff only prints when there’s a difference.
  • Exit code 1 will stop set -e scripts. Guard comparisons: diff -q a b || true, or test with if.
  • Diff is line-based. For binary files use cmp (cmp); diff’s output on binaries is Binary files ... differ at best.
  • Reformatted files hide real changes. Add -w or -B when an editor may have moved whitespace or blank lines around.

CommandDescription
cmpCompare two files byte by byte
diffoscopeDeeply compare archives, documents, and binaries
git diffThe same comparison inside Git, with history
md5sum / sha256sumHash-based “are they identical?” check for large files
rsyncCompare directories and synchronise the differences

diff Cheatsheet

CommandWhat It Does
diff a.txt b.txtShow line-by-line differences
diff -u a.txt b.txtUnified format (the standard patch format)
diff -q a.txt b.txtOnly say whether they differ
diff -y a bSide-by-side comparison
diff -y --suppress-common-lines a bSide-by-side, differences only
diff -r dir1 dir2Compare two directory trees recursively
diff -qr dir1 dir2…names only — fast drift detection
diff -u -w a bIgnore white-space-only changes
diff -u old new > patch.diffGenerate a patch
echo $? after diff0 same, 1 differ, 2 error