diff
Compare files line by line
TLDR
Compare files (lists changes to turn old_file into new_file)
Compare files, ignoring white spaces
Compare files, showing the differences side by side
Compare files, showing the differences in unified format (as used by git diff)
Compare directories recursively (shows names for differing files/directories as well as changes made to files)
Compare directories, only showing the names of files that differ
Create a patch file for Git from the differences of two text files, treating nonexistent files as empty
Compare files, showing output in color and try hard to find smaller set of changes
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.