Skip to content

diff Command Cheat Sheet

diff compares two files line by line. It is one of the oldest and most important Unix tools, serving as the engine behind patch management and version control concepts.


Synopsis

diff [OPTION]... FILES

Basic Usage

Compare Two Files

diff file1.txt file2.txt

Output Format (default):

1c1
< old_line
---
> new_line
- <: Line from first file. - >: Line from second file. - 1c1: Line 1 changed to line 1.


Unified Format (-u)

The industry standard format (used by git diff). It provides context lines (default 3) around changes, making them easier to read.

diff -u file1.txt file2.txt

Output:

--- file1.txt   2026-02-14 12:00:00
+++ file2.txt   2026-02-14 12:05:00
@@ -1,3 +1,3 @@
-old_line
+new_line
 context line 1
 context line 2


Side-by-Side Comparison (-y)

Displays files in two columns. Useful for visual comparison.

diff -y file1.txt file2.txt

Suppress Common Lines

Showing identical lines might be noisy. Use --suppress-common-lines to see only changes.

diff -y --suppress-common-lines file1.txt file2.txt

Adjust Column Width

If lines are wrapping or getting cut off:

diff -y -W 200 file1.txt file2.txt

Ignoring Differences

Sometimes whitespace or case doesn't matter.

Ignore All Whitespace (-w)

Ignores spaces, tabs, and empty lines. Treat "a = b" and "a=b" as identical.

diff -w file1.c file2.c

Ignore Whitespace Changes (-b)

Ignores changes in the amount of whitespace (e.g., one space vs two spaces), but not whitespace vs no whitespace.

diff -b file1.txt file2.txt

Ignore Case (-i)

Case insensitive comparison.

diff -i file1.txt file2.txt

Ignore Blank Lines (-B)

Ignores lines that are empty or contain only whitespace.

diff -B file1.txt file2.txt

Directory Comparison (-r)

Recursively compare two directories.

diff -r src_v1/ src_v2/

Report Only if Files Differ (-q)

"Brief" mode. Doesn't show the actual content differences, just which files differ.

diff -rq src_v1/ src_v2/
Output:
Files src_v1/main.c and src_v2/main.c differ
Only in src_v2/: innovative_feature.c


Patch Management

diff creates patches; patch applies them.

Create a Patch

diff -u original.c updated.c > changes.patch

Apply a Patch

patch original.c < changes.patch
# original.c is now identical to updated.c

Color Output

Modern GNU diff supports color.

diff --color=auto file1 file2

If not available, install colordiff and alias it:

alias diff='colordiff'


Process Substitution

Compare the output of two commands directly without creating temporary files.

diff <(ls -1 dir1) <(ls -1 dir2)

Compare local file with remote file (via SSH):

diff local.conf <(ssh user@remote "cat /etc/remote.conf")

Practical Examples

Check Config Changes Before Restart

diff -u /etc/nginx/nginx.conf.bak /etc/nginx/nginx.conf

Verify Backup Integrity

Comparison of a directory and its backup.

diff -r /var/www/html /backup/www/html

Compare Sorted Files

If lines are in different order, diff fails. Sort them first.

diff <(sort file1.txt) <(sort file2.txt)

Exit Status

Code Meaning
0 No differences (Files check out)
1 Differences found
2 Trouble (File not found, error)

Notes

  • sdiff: An older side-by-side merge tool, similar to diff -y.
  • vimdiff: Opens files in Vim in vertical split mode (very powerful).
  • git diff: Uses a slightly enhanced version of unified diff but works on git objects, not just files.