diff(1)
Compare two files line by line and show their differences.
Synopsis
diff [OPTION]... FILE1 FILE2Description
diff compares two files and outputs the lines that differ. It shows which lines need to be changed in FILE1 to make it match FILE2. Output uses a compact format where 'a' means add, 'd' means delete, and 'c' means change.
By default, diff uses an abbreviated output format. Use -u for unified diff (showing context around changes) or -y for side-by-side comparison, which are more readable for most users.
Common options
| Flag | What it does |
|---|---|
-u | unified diff format; shows 3 lines of context around each change (preferred format) |
-y | side-by-side comparison; columns show both files with differences marked |
-w | ignore all whitespace; treats lines with different spacing as identical |
-b | ignore changes in whitespace amount; treats multiple spaces as one |
-i | ignore case differences; treats uppercase and lowercase as identical |
-r | recursive; compare directories and their contents recursively |
-q | quiet; only report if files differ, not the actual differences |
-c | context format; shows differences with 3 lines of context (older format) |
--color=auto | colorize output (red for deletions, green for additions) |
Examples
Basic comparison; shows line numbers and what changed (e.g., '3c3' means line 3 changed)
diff file1.txt file2.txtUnified diff with context; standard for reviewing code changes and patch files
diff -u original.txt modified.txt | lessSide-by-side view in 150 character width; easier to spot differences visually
diff -y --width=150 old.conf new.confIgnore all whitespace differences; useful when formatting changed but logic didn't
diff -w config1.sh config2.shCompare two directories recursively and show which files exist in only one
diff -r /etc/dir1 /etc/dir2 | grep '^Only'Quick check if files differ; only reports changed files, not the details
diff -q *.txt backup/*.txtCreate a patch file that can be applied with patch command
diff -u file.old file.new > changes.patch