diff
Overview
The diff
command compares files line by line and displays the differences. It’s essential for version control, code review, and file comparison tasks.
Syntax
diff [options] file1 file2
diff [options] directory1 directory2
Common Options
Option | Description |
---|---|
-u |
Unified diff format |
-c |
Context diff format |
-i |
Ignore case differences |
-w |
Ignore whitespace |
-b |
Ignore changes in whitespace |
-B |
Ignore blank lines |
-r |
Recursive directory comparison |
-q |
Brief output (only if files differ) |
-s |
Report identical files |
-y |
Side-by-side comparison |
--color |
Colorize output |
Output Formats
Format | Description |
---|---|
Normal | Default format with line numbers |
Unified (-u) | Git-style format |
Context (-c) | Shows context around changes |
Side-by-side (-y) | Two-column comparison |
Key Use Cases
- Compare file versions
- Code review
- Configuration changes
- Backup verification
- Patch creation
Examples with Explanations
Example 1: Basic Comparison
diff file1.txt file2.txt
Shows differences between two files
Example 2: Unified Format
diff -u original.txt modified.txt
Shows differences in unified format (like Git)
Example 3: Directory Comparison
diff -r dir1/ dir2/
Recursively compares two directories
Understanding Output
Normal format symbols: - a
- Added lines - d
- Deleted lines - c
- Changed lines - <
- Lines from first file - >
- Lines from second file
Example:
2c2
< old line
---
> new line
Common Usage Patterns
Ignore whitespace:
diff -w file1.txt file2.txt
Side-by-side view:
diff -y file1.txt file2.txt
Quick check if files differ:
diff -q file1.txt file2.txt
Advanced Options
Option | Description |
---|---|
--exclude=pattern |
Exclude files matching pattern |
--exclude-from=file |
Exclude patterns from file |
-x pattern |
Exclude files matching pattern |
-N |
Treat absent files as empty |
-a |
Treat all files as text |
--strip-trailing-cr |
Strip carriage returns |
Patch Creation
Create patches for later application:
diff -u original.txt modified.txt > changes.patch
Apply patches:
patch original.txt < changes.patch
Performance Analysis
- Efficient for text files
- Memory usage scales with file size
- Good for moderate-sized files
- Consider alternatives for binary files
- Fast for small differences
Additional Resources
Best Practices
- Use unified format for patches
- Ignore irrelevant whitespace
- Use recursive mode for directories
- Consider binary file handling
- Use with version control systems
Directory Comparison
Compare structures:
diff -r --brief dir1/ dir2/
Exclude files:
diff -r --exclude="*.log" dir1/ dir2/
Show only differences:
diff -r -q dir1/ dir2/
Integration Examples
With git:
git diff > changes.patch
With find:
diff <(find dir1 -type f | sort) <(find dir2 -type f | sort)
Configuration management:
diff -u /etc/config.orig /etc/config
Scripting Applications
Backup verification:
if diff -q original.txt backup.txt > /dev/null; then echo "Backup is identical" fi
Configuration monitoring:
diff /etc/passwd /etc/passwd.bak || echo "Password file changed"
Automated testing:
diff expected_output.txt actual_output.txt || exit 1
Special Cases
Binary files:
diff -q binary1 binary2
Large files:
diff --speed-large-files file1 file2
Case-insensitive:
diff -i file1.txt file2.txt
Troubleshooting
- Binary file warnings
- Memory issues with large files
- Character encoding problems
- Permission denied errors
- Directory structure differences
Output Redirection
Save differences:
diff file1.txt file2.txt > differences.txt
Suppress output:
diff file1.txt file2.txt > /dev/null
Error handling:
diff file1.txt file2.txt 2>&1 | tee diff.log
Color Output
Modern diff versions support color:
diff --color=always file1.txt file2.txt
Environment variable:
export DIFF_COLORS="old=31:new=32:hunk=36"