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 [options] file1 file2

PARAMETERS

-q
    Report only when files differ, ignoring details.

-s
    Report when two files are the same.

-i
    Ignore case differences.

-w
    Ignore all whitespace.

-b
    Ignore changes in the amount of whitespace.

-B
    Ignore changes where lines are all blank.

-u
    Output in unified diff format.

-c
    Output in context diff format.

-r
    Recursively compare directories.

-N
    Treat absent files as empty.

--ignore-space-change
    Ignore differences in amount of white space.

--ignore-blank-lines
    Ignore differences where lines are all blank.

--from-file=FILE
    Compare FILE to all operands; FILE can be a directory.

--to-file=FILE
    Compare all operands to FILE; FILE can be a directory.

DESCRIPTION

The diff command is a utility used to compare two files and highlight the differences between them. It analyzes the files and outputs a set of instructions that, if applied to the first file, would transform it into the second file. These instructions are typically in the form of additions, deletions, and changes.
diff is an essential tool for software development, version control, and system administration. It helps identify modifications made to source code, configuration files, and other text-based data. Many versions of diff exist, including implementations that offer various output formats (unified, context, etc.) and features for ignoring whitespace, case differences, or specific line patterns. diff is widely used for creating patches to distribute code changes, allowing developers to easily apply updates without distributing entire files. Its versatility and efficiency make it a fundamental part of the Unix/Linux toolchain. Different output formats are commonly used by patch utilities, such as unified diff or context diff format. This command is also useful for finding subtle differences in files.

CAVEATS

The output of diff can be hard to read without experience. Different versions of diff may produce slightly different output formats. Handling binary files effectively requires tools beyond simple diff.

OUTPUT FORMAT

The standard diff output format represents changes using symbols like 'a' (add), 'c' (change), and 'd' (delete), along with line numbers indicating the affected sections in the files. The unified diff format, commonly used with the '-u' option, presents a more readable and compact representation of the differences with context lines showing the surrounding content.

EXIT STATUS

The diff command returns an exit status indicating the result of the comparison. A status of 0 means the files are identical, 1 means the files are different, and 2 indicates an error.

HISTORY

diff has been a standard Unix utility since the early 1970s. Its development was driven by the need to track and manage changes in source code and other text-based files. The original implementations were relatively simple, but over time, diff evolved to include more sophisticated algorithms and output formats, such as unified diff, to better support patching and version control. It is part of the POSIX standard. The command is fundamental to the operation of source control systems like Git.

SEE ALSO

patch(1), cmp(1), comm(1)

Copied to clipboard