Overview
diff -u compares the contents of two files and outputs the differences in a unified format. This format displays changed lines along with a few lines of surrounding context, making it easy to understand what parts have been changed and how. It is an essential tool, especially for source code management and tracking changes in configuration files.
Key Features
- Displays differences in Unified format
- Provides context for changes
- Useful for code reviews and version control
- Clearly distinguishes additions/deletions/modifications
Key Options
The diff command offers various options; here, we explain options commonly used with the `-u` option.
Output Format
Comparison Behavior
Generated command:
Try combining the commands.
Description:
`diff` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Various usage examples of the diff -u command.
Compare Differences Between Two Files in Unified Format
diff -u file1.txt file2.txt
Outputs the differences between file1.txt and file2.txt in unified format.
Recursively Compare Files in Two Directories
diff -ur dir1 dir2
Recursively compares all files within dir1 and dir2 directories and outputs the results in unified format.
Compare Files Ignoring Whitespace Changes
diff -uw file1.txt file2.txt
Compares files, ignoring changes in whitespace characters (spaces, tabs, etc.) to focus on actual content differences.
Compare Files Ignoring Lines Matching a Specific Pattern
diff -u -I '^#' config1.conf config2.conf
Compares config1.conf and config2.conf files, ignoring lines that start with a comment character (#).
Save Unified Output to a Patch File
diff -u old_file.txt new_file.txt > changes.patch
Outputs the differences between two files in unified format and saves the result to a file named 'changes.patch'. This file can be applied using the 'patch' command.
Tips & Notes
Tips for effectively interpreting and utilizing the output of diff -u.
How to Interpret Unified Output
The output of diff -u uses the following symbols to indicate changes:
- --- (three hyphens): Indicates the path and timestamp of the original (old) file.
- +++ (three pluses): Indicates the path and timestamp of the new file.
- @@ -OLD_START,OLD_COUNT +NEW_START,NEW_COUNT @@: This is the 'hunk header', indicating the line numbers and ranges in the files where changes occurred.
- - (hyphen): A line that exists only in the original file, signifying a deleted line.
- + (plus): A line that exists only in the new file, signifying an added line.
- (space): A line that exists in both files, signifying a context line with no changes.
Integration with the patch Command
The output generated by diff -u can be used as input for the `patch` command. This allows you to easily apply or revert changes from one file to another. For example, after creating a patch file with `diff -u old_file new_file > changes.patch`, you can apply the changes from `changes.patch` to `old_file` using `patch -p0 < changes.patch` to make it identical to `new_file`.