diff Command Guide
The diff command compares files line by line. Learn how to identify differences between files and directories.
6 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
diff f1 f2Compare filesdiff -u f1 f2Unified formatdiff -y f1 f2Side by sideOptions
-iIgnore case-wIgnore whitespace-BIgnore blank linesDirectory
diff -r dir1 dir2Recursive comparediff -rq dir1 dir2Only show diff files--exclude=*.logExclude patternPatch
diff -u f1 f2 > p.patchCreate patchpatch < p.patchApply patchpatch -R < p.patchReverse patchDownloadable Image Preview
Failed to generate preview
Basic Usage
diff shows differences between two files.
bash
diff file1.txt file2.txtNo output means the files are identical.
Common Options
diff Options
| -u | Unified format (most common) |
| -c | Context format |
| -y | Side-by-side comparison |
| -i | Ignore case differences |
| -w | Ignore all whitespace |
| -b | Ignore whitespace changes |
| -r | Recursively compare directories |
| -q | Report only if files differ |
| --color | Colorize output |
Unified Format (-u)
The most common format, used for patches and version control.
bash
diff -u file1.txt file2.txtdiff
--- file1.txt 2024-01-15 10:00:00
+++ file2.txt 2024-01-15 11:00:00
@@ -1,4 +1,4 @@
line 1
-line 2 old
+line 2 new
line 3
line 4Lines starting with - are removed, + are added.
Side-by-Side (-y)
bash
diff -y file1.txt file2.txt
# With specific width
diff -y -W 80 file1.txt file2.txttext
line 1 line 1
line 2 old | line 2 new
line 3 line 3Ignoring Whitespace
bash
# Ignore all whitespace
diff -w file1.txt file2.txt
# Ignore changes in whitespace amount
diff -b file1.txt file2.txt
# Ignore blank lines
diff -B file1.txt file2.txtComparing Directories
bash
# Recursive comparison
diff -r dir1/ dir2/
# Only report if different
diff -rq dir1/ dir2/
# Exclude patterns
diff -r --exclude="*.log" dir1/ dir2/Colorized Output
bash
diff --color file1.txt file2.txt
diff --color=always file1.txt file2.txt | less -RCreating and Applying Patches
bash
# Create a patch file
diff -u original.txt modified.txt > changes.patch
# Apply a patch
patch original.txt < changes.patch
# Reverse a patch
patch -R original.txt < changes.patchTip
Use
-u format for patches as it provides context and is the standard format.Practical Examples
Compare configuration files
bash
diff -u /etc/nginx/nginx.conf nginx.conf.newCheck if files are different
bash
if diff -q file1.txt file2.txt > /dev/null; then
echo "Files are identical"
else
echo "Files differ"
fiCompare with git version
bash
diff file.txt <(git show HEAD:file.txt)Compare command outputs
bash
diff <(ls dir1) <(ls dir2)Show context around changes
bash
# Show 5 lines of context
diff -U5 file1.txt file2.txtAlternative Tools
Diff Alternatives
| colordiff | Wrapper for colored output |
| vimdiff | Visual diff in Vim |
| meld | GUI diff tool |
| delta | Modern pager for diff output |
| diff3 | Compare three files |
bash
# Use colordiff for colors
colordiff file1.txt file2.txt
# Visual diff in vim
vimdiff file1.txt file2.txt
# Three-way diff
diff3 mine.txt original.txt yours.txtExit Codes
Exit Codes
| 0 | Files are identical |
| 1 | Files are different |
| 2 | Error occurred |
Summary
diff is essential for comparing files. Key takeaways:
- Use
diff -ufor unified format (patches) - Use
diff -yfor side-by-side view - Use
diff -rfor directories - Use
diff -qto only check if different - Use
--colorfor colored output