Diff Cheatsheet #
The BSD diff
command compares files line by line and shows the differences between them.
Basic Commands #
Command/Option | Example | Description |
---|---|---|
diff | diff file1.txt file2.txt | Compare two files line by line |
diff -u | diff -u file1.txt file2.txt | Unified format for differences (-u for unified) |
diff -c | diff -c file1.txt file2.txt | Context format for differences (-c for context) |
diff -i | diff -i file1.txt file2.txt | Ignore case differences (-i for ignore case) |
diff -w | diff -w file1.txt file2.txt | Ignore all white space (-w for ignore white space) |
diff -b | diff -b file1.txt file2.txt | Ignore changes in the amount of white space (-b for ignore space changes) |
diff -r | diff -r dir1 dir2 | Recursively compare directories (-r for recursive) |
diff -N | diff -N file1.txt file2.txt | Treat absent files as empty (-N for new files) |
Advanced Commands #
Command/Option | Example | Description |
---|---|---|
diff -y | diff -y file1.txt file2.txt | Display differences side by side (-y for side-by-side) |
diff --side-by-side | diff --side-by-side file1.txt file2.txt | Display differences side by side with --side-by-side |
diff -u -p | diff -u -p file1.txt file2.txt | Unified format with C function names (-p for C function names) |
diff -q | diff -q file1.txt file2.txt | Report only if files differ (-q for quiet) |
diff -r -x | diff -r -x '*.log' dir1 dir2 | Exclude files matching a pattern when comparing directories (-x for exclude) |
diff -u -N | diff -u -N file1.txt file2.txt | Unified format with treating absent files as empty (-N for new files) |
This cheatsheet covers the essential BSD diff
commands and options for comparing files and directories.