LinuxCommandLibrary

diff

Compare files line by line

TLDR

Compare files (lists changes to turn old_file into new_file)

$ diff [old_file] [new_file]
copy

Compare files, ignoring white spaces
$ diff [[-w|--ignore-all-space]] [old_file] [new_file]
copy

Compare files, showing the differences side by side
$ diff [[-y|--side-by-side]] [old_file] [new_file]
copy

Compare files, showing the differences in unified format (as used by git diff)
$ diff [[-u|--unified]] [old_file] [new_file]
copy

Compare directories recursively (shows names for differing files/directories as well as changes made to files)
$ diff [[-r|--recursive]] [old_directory] [new_directory]
copy

Compare directories, only showing the names of files that differ
$ diff [[-r|--recursive]] [[-q|--brief]] [old_directory] [new_directory]
copy

Create a patch file for Git from the differences of two text files, treating nonexistent files as empty
$ diff [[-a|--text]] [[-u|--unified]] [[-N|--new-file]] [old_file] [new_file] > [diff.patch]
copy

Compare files, showing output in color and try hard to find smaller set of changes
$ diff [[-d|--minimal]] --color=always [old_file] [new_file]
copy

SYNOPSIS

diff [OPTION]... -- FILE1 [FILE2] | DIR1 DIR2

PARAMETERS

-a, --text
    Treat all files as text

-b, --ignore-space-change
    Ignore changes in amount of whitespace

-B, --ignore-blank-lines
    Ignore changes whose lines are all blank

-d, --minimal
    Minimize set of changes found

-i, --ignore-case
    Consider upper- and lower-case the same

-q, --brief
    Report only if files differ

-r, --recursive
    Recursively compare subdirectories

-N, --new-file
    Treat absent files as empty

-s, --report-identical-files
    Report identical files

-S FILE, --starting-file=FILE
    Start scanning at FILE in dirs

-u, -U NUM, --unified[=NUM]
    Unified format with NUM context lines

-c, -C NUM, --context[=NUM]
    Context format with NUM lines

-w, --ignore-all-space
    Ignore all whitespace

-y, --side-by-side
    Side-by-side columnar output

-W NUM, --width=NUM
    Width for side-by-side output

--color[=WHEN]
    Colorize output (never, always, auto)

-L LABEL, --label=LABEL
    Use LABEL for file header

-R
    Reverse direction of comparison

--help
    Display usage summary

-v, --version
    Output version information

DESCRIPTION

The diff command compares files or directories line by line, highlighting differences in a compact format. Essential for developers, it reveals changes between file versions, aiding patch creation and code reviews.

Default output shows differing line numbers prefixed by '<' (first file) or '>' (second file). Formats include:
- Normal: Concise changes.
- Context (-c): Surrounding lines for clarity.
- Unified (-u): Popular for patches, combining context.
- Side-by-side (-y): Columnar view.

Directories are scanned recursively (-r), comparing same-named files. Options ignore whitespace (-w, -b), case (-i), blank lines (-B), or report briefly (-q). It treats files as text by default (-a) and handles absent files as empty (-N).

Outputs to stdout; pipe to patch for application. Exit codes: 0 (identical), 1 (differ), 2 (error). Widely used in Git, SVN for diffs. Limitations include slowness on huge files without tuning.

CAVEATS

diff works best on text; binaries always "differ" unless identical. Large files/directories slow without -q or --brief. Recursive scans (-r) memory-intensive on deep trees. Use cmp(1) for byte-level binary checks.

EXIT STATUS

0: Files identical
1: Files differ
2: Error (e.g., permissions, missing files)

EXAMPLES

diff -u old.c new.c > patchfile
diff -r dir1/ dir2/ for dirs
diff -y -W 80 file1 file2 side-by-side

HISTORY

Created 1970 by Douglas McIlroy at Bell Labs for early Unix. GNU diffutils (1988+) by Randy Smith adds unified output, recursion, color. Evolved for VCS integration; POSIX standardized subset.

SEE ALSO

cmp(1), comm(1), diff3(1), patch(1), sdiff(1), vimdiff(1)

Copied to clipboard