diff: Compare Files Line by Line
August 12th, 2024 1:43 PM Mr. Q Categories: Command
Description:
The diff
command compares two files line by line and outputs the differences between them. It is useful for identifying changes between files, often used in version control systems.
Command:
diff file1.txt file2.txt
Sample Input
file1.txt
:
apple
banana
cherry
file2.txt
:
banana
cherry
date
Sample Output:
1d0
< apple
3a3
> date
Explanation:
1d0
: Line 1 is present infile1.txt
but not infile2.txt
.< apple
: The lineapple
is infile1.txt
but not infile2.txt
.3a3
: Line 3 offile2.txt
is added after line 3 offile1.txt
.> date
: The linedate
is infile2.txt
but not infile1.txt
.
Options:
-u
: Unified format, showing a few lines of context around the differences.-c
: Context format, similar to unified but with more context lines.-i
: Ignore case differences.
Example with -u
Option:
To show the differences in unified format:
diff -u file1.txt file2.txt
Sample Output with -u
:
--- file1.txt
+++ file2.txt
@@ -1,3 +1,3 @@
apple
banana
-cherry
+date
This command is helpful for patch creation, code reviews, and file comparison tasks.