Terminal GuideTerminal Guide

diff Command Guide

The diff command compares files line by line. Learn how to identify differences between files and directories.

6 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

diff f1 f2Compare files
diff -u f1 f2Unified format
diff -y f1 f2Side by side

Options

-iIgnore case
-wIgnore whitespace
-BIgnore blank lines

Directory

diff -r dir1 dir2Recursive compare
diff -rq dir1 dir2Only show diff files
--exclude=*.logExclude pattern

Patch

diff -u f1 f2 > p.patchCreate patch
patch < p.patchApply patch
patch -R < p.patchReverse patch

Downloadable Image Preview

Failed to generate preview

Basic Usage

diff shows differences between two files.

bash
diff file1.txt file2.txt

No output means the files are identical.

Common Options

diff Options

-uUnified format (most common)
-cContext format
-ySide-by-side comparison
-iIgnore case differences
-wIgnore all whitespace
-bIgnore whitespace changes
-rRecursively compare directories
-qReport only if files differ
--colorColorize output

Unified Format (-u)

The most common format, used for patches and version control.

bash
diff -u file1.txt file2.txt
diff
--- 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 4

Lines 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.txt
text
line 1                           line 1
line 2 old                     | line 2 new
line 3                           line 3

Ignoring 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.txt

Comparing 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 -R

Creating 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.patch
Tip
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.new

Check if files are different

bash
if diff -q file1.txt file2.txt > /dev/null; then
    echo "Files are identical"
else
    echo "Files differ"
fi

Compare 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.txt

Alternative Tools

Diff Alternatives

colordiffWrapper for colored output
vimdiffVisual diff in Vim
meldGUI diff tool
deltaModern pager for diff output
diff3Compare 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.txt

Exit Codes

Exit Codes

0Files are identical
1Files are different
2Error occurred

Summary

diff is essential for comparing files. Key takeaways:

  • Use diff -u for unified format (patches)
  • Use diff -y for side-by-side view
  • Use diff -r for directories
  • Use diff -q to only check if different
  • Use --color for colored output

Related Articles