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
diff [OPTIONS] DIRECTORY1 DIRECTORY2
diff [OPTIONS] FILE DIRECTORY

PARAMETERS

-u or --unified
    Output in unified format. This is a compact way to show changes, commonly used for creating patch files.

-c or --context
    Output in context format. Shows several lines of context around each change for better readability.

-N or --new-file
    Treat absent files as empty. Useful when comparing directories where one file might be missing.

-r or --recursive
    Recursively compare subdirectories when comparing two directories.

-q or --brief
    Output only a summary, stating if files differ or are identical, without detailed diff output.

-y or --side-by-side
    Output in a two-column format, showing FILE1 on the left and FILE2 on the right for visual comparison.

-w or --ignore-all-space
    Ignore all whitespace changes when comparing lines.

-B or --ignore-blank-lines
    Ignore changes where lines are blank.

-I REGEXP or --ignore-matching-lines=REGEXP
    Ignore changes to lines that match the provided regular expression.

--no-dereference
    Do not follow symbolic links; compare the links themselves rather than their targets.

DESCRIPTION

The diff command is a fundamental Unix utility used to compare two files or directories line by line. It outputs the differences between them, indicating which lines have been added, deleted, or changed. This utility is indispensable for tracking changes in source code, configuration files, and text documents. diff can generate various output formats, including the traditional "normal" format, "context" format (showing lines around the changes), and the highly popular "unified" format, which is compact and commonly used with patch to apply changes. When comparing directories, diff recursively identifies differing files and reports on them. It's a cornerstone tool in version control systems and software development workflows.

CAVEATS

diff performs line-based comparisons. It does not understand the semantic meaning of code or text, only character differences on a line.
For binary files, diff typically reports only that the files differ without showing content changes.
Comparing very large files or directories can be resource-intensive and slow.

EXIT STATUS

diff exits with a status of 0 if the files are identical, 1 if they differ, and 2 if an error occurred. This makes it suitable for use in scripts to conditionally execute commands based on file differences.

OUTPUT FORMATS OVERVIEW

The default normal format shows additions (a), deletions (d), and changes (c) with line numbers. The context format (-c) provides more surrounding lines for clarity. The unified format (-u) is highly compact, showing changes prefixed with + for additions, - for deletions, and a space for unchanged lines; it's often used to create patch files. The side-by-side format (-y) presents the files in two columns for easy visual comparison.

HISTORY

The diff utility dates back to the early days of Unix, first appearing in Research Unix, likely developed in the 1970s. It was created by Douglas McIlroy at Bell Labs. Its fundamental algorithm, based on the longest common subsequence problem, revolutionized how changes in text files could be tracked and applied, paving the way for modern version control systems. It has been a standard Unix utility ever since and is part of the POSIX standard.

SEE ALSO

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

Copied to clipboard