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
.difffile and need to understand it. - Two directories have drifted and you need to know what differs.
- Scripts depend on it:
diff -qis 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,4means: 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 differotherwise. - 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=120for 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 -qrshortens 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:
| Flag | Ignores |
|---|---|
-w | All white space |
-b | Changes in amount of white space |
-i | Case differences (text) |
-B | Blank lines |
-E, -F | Trailing/end whitespace and CR line endings (CRLF vs LF) |
diff -u -wanswers “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, orgit apply my-changes.patchinside 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;diffonly prints when there’s a difference. - Exit code 1 will stop
set -escripts. Guard comparisons:diff -q a b || true, or test withif. - Diff is line-based. For binary files use
cmp(cmp); diff’s output on binaries isBinary files ... differat best. - Reformatted files hide real changes. Add
-wor-Bwhen an editor may have moved whitespace or blank lines around.
Related Commands
| Command | Description |
|---|---|
| cmp | Compare two files byte by byte |
| diffoscope | Deeply compare archives, documents, and binaries |
git diff | The same comparison inside Git, with history |
| md5sum / sha256sum | Hash-based “are they identical?” check for large files |
| rsync | Compare directories and synchronise the differences |
diff Cheatsheet
| Command | What It Does |
|---|---|
diff a.txt b.txt | Show line-by-line differences |
diff -u a.txt b.txt | Unified format (the standard patch format) |
diff -q a.txt b.txt | Only say whether they differ |
diff -y a b | Side-by-side comparison |
diff -y --suppress-common-lines a b | Side-by-side, differences only |
diff -r dir1 dir2 | Compare two directory trees recursively |
diff -qr dir1 dir2 | …names only — fast drift detection |
diff -u -w a b | Ignore white-space-only changes |
diff -u old new > patch.diff | Generate a patch |
echo $? after diff | 0 same, 1 differ, 2 error |