Overview
diff compares changes between two files or directories, showing which lines have been added, deleted, or modified. Output formats include unified, context, and side-by-side. It can also adjust comparison sensitivity for whitespace or case.
Key Features
- Line-by-line comparison and human-readable formats provided
- Recursive comparison between directories possible (-r)
- Various ignore rules for whitespace, case, empty lines, etc.
- Supports unified format (-u) suitable for patch (.patch) generation
- Offers optimized options for large file comparison performance
Example Scenarios
- Code review: Extracting only changed files as a patch
- Configuration file change tracking: Comparing settings before and after deployment
- Backup/snapshot comparison: Checking differences between specific points in time
- Detecting unintended changes in document/data pre-processing stages
- Security checks: Verifying suspicious changes in binaries/scripts deployed from external sources
diff Output Format
The output format of diff indicates instructions for changing the first file to the second file.
- `a` (add): Lines present in the second file but not in the first file have been **added**. (e.g., `1a2`)
- `c` (change): The corresponding lines in both files have been **changed**. (e.g., `1c2`)
- `d` (delete): Lines present in the first file but not in the second file have been **deleted**. (e.g., `1d2`)
Key Options
Frequently used options grouped by purpose.
1) Basic Behavior & Output Format
2) Specify Comparison Scope/Targets
3) Output Control / Ignore Rules
4) Help/Version
Generated command:
Try combining the commands.
Description:
`diff` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Common comparison scenarios organized as examples.
Generate Patch in Unified Format
diff -u old.conf new.conf > change.patch
Recursive Directory Comparison
diff -ruN ./v1 ./v2
Compare Ignoring Whitespace Differences
diff -uw src_old.c src_new.c
Side-by-Side View + Hide Common Lines
diff -y --suppress-common-lines a.txt b.txt
Exit Codes
Refer to these when handling conditional logic in scripts.
Code | Meaning |
---|---|
0 | Two targets are identical |
1 | Differences found |
2 | Error occurred (e.g., usage error, file access denied) |
Installation
Most distributions include diffutils by default, but if not, install the diffutils package.
Debian/Ubuntu
sudo apt update && sudo apt install -y diffutils
RHEL/CentOS/Fedora
sudo dnf install -y diffutils
Arch Linux
sudo pacman -S --needed diffutils
openSUSE
sudo zypper install -y diffutils
Tips & Cautions
Points often overlooked in practice.
- The patch format commonly uses -u as the standard. Git also internally uses a similar unified format.
- Overuse of whitespace handling options (-w, -b, -B) can obscure actual meaningful changes.
- Differences in Windows/Linux newlines can be easily compared by normalizing with --strip-trailing-cr.
- When comparing directories, use -r along with -x/-X (exclude rules) to filter out unnecessary files.
- If you only need to quickly check for existence, use the -q option. It will not output detailed content.