$linuxjunkies
>

diff(1)

Compare two files line by line and show their differences.

UbuntuDebianFedoraArch

Synopsis

diff [OPTION]... FILE1 FILE2

Description

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

FlagWhat it does
-uunified diff format; shows 3 lines of context around each change (preferred format)
-yside-by-side comparison; columns show both files with differences marked
-wignore all whitespace; treats lines with different spacing as identical
-bignore changes in whitespace amount; treats multiple spaces as one
-iignore case differences; treats uppercase and lowercase as identical
-rrecursive; compare directories and their contents recursively
-qquiet; only report if files differ, not the actual differences
-ccontext format; shows differences with 3 lines of context (older format)
--color=autocolorize 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.txt

Unified diff with context; standard for reviewing code changes and patch files

diff -u original.txt modified.txt | less

Side-by-side view in 150 character width; easier to spot differences visually

diff -y --width=150 old.conf new.conf

Ignore all whitespace differences; useful when formatting changed but logic didn't

diff -w config1.sh config2.sh

Compare 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/*.txt

Create a patch file that can be applied with patch command

diff -u file.old file.new > changes.patch

Related commands