diff Command
Intermediate Text Processing man(1)Compare files line by line
👁 9 views
📅 Updated: Mar 15, 2026
SYNTAX
diff [OPTION]... FILE1 FILE2
What Does diff Do?
The diff command compares two files line by line and outputs the differences between them. It is essential for code review, configuration management, patch creation, and troubleshooting.
diff supports several output formats: normal (default), unified (-u, most commonly used), context (-c), and side-by-side (-y). The unified format is standard for code patches, version control diffs, and code review tools.
Beyond simple file comparison, diff can compare directories recursively, ignore specific types of changes (whitespace, case), and generate patch files that can be applied with the patch command to transform one file into another.
diff supports several output formats: normal (default), unified (-u, most commonly used), context (-c), and side-by-side (-y). The unified format is standard for code patches, version control diffs, and code review tools.
Beyond simple file comparison, diff can compare directories recursively, ignore specific types of changes (whitespace, case), and generate patch files that can be applied with the patch command to transform one file into another.
Options & Flags
| Option | Description | Example |
|---|---|---|
| -u | Unified diff format (most readable, standard for patches) | diff -u old.conf new.conf |
| -r | Recursively compare directories | diff -r dir1/ dir2/ |
| -y | Side-by-side output | diff -y file1.txt file2.txt |
| -q | Report only whether files differ | diff -q config.old config.new |
| -i | Ignore case differences | diff -i file1.txt file2.txt |
| -w | Ignore all whitespace | diff -w old.py new.py |
| -B | Ignore blank line changes | diff -B old.txt new.txt |
| --color | Colorize output (GNU diff) | diff --color -u old.conf new.conf |
Practical Examples
#1 Unified diff of two files
Shows differences in unified format with + for additions and - for removals.
$ diff -u config.old config.new
Output:
--- config.old
+++ config.new
@@ -1,3 +1,3 @@
-port=80
+port=443
#2 Compare directories
Recursively compares two directory trees, reporting only which files differ.
$ diff -rq project-v1/ project-v2/#3 Side-by-side comparison
Shows both files side by side with differences marked.
$ diff -y --width=120 file1.txt file2.txt#4 Create a patch file
Creates a patch file that can be applied with the patch command.
$ diff -u original.c modified.c > fix.patch#5 Apply a patch
Applies the differences from a patch file to transform the original.
$ patch < fix.patch#6 Colorized diff
Shows differences with color highlighting for easy reading.
$ diff --color -u /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bakTips & Best Practices
Use colordiff for better visibility: Install colordiff for automatic colorization: diff -u file1 file2 | colordiff. Or pipe to less -R for paged colored output.
Exit codes: diff returns 0 if files are identical, 1 if they differ, and 2 on error. Useful in scripts: if diff -q file1 file2 > /dev/null; then echo "Same"; fi
Binary files: diff is designed for text files. For binary files, use cmp for byte-level comparison or xxd for hex dump comparison.
Frequently Asked Questions
How do I compare two files in Linux?
Use diff file1.txt file2.txt. For the most readable format, use diff -u file1 file2 (unified format with +/- markers).
How do I create and apply a patch?
Create: diff -u original modified > patch.diff. Apply: patch < patch.diff (or patch original < patch.diff).
How do I compare two directories?
Use diff -rq dir1/ dir2/ to recursively compare directories. -q shows only which files differ without showing the actual differences.
Related Commands
More Text Processing Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →