Linux: Using diff to compare files
diff reports the differences between two files.
diff [options] <i>file1</i> <i>file2</i>
Simple Usage Example
A common line.
Really common line.
One more common line.
A almost common line.
Really common line.
Really not common line.
One further line.
One more common line.
$ diff test1 test2
< A common line.
> A almost common line.
> Really not common line.
> One further line.
< Only here.
What does that mean? First of all this output can be used as a script for ed (see example ). Lines
from first file are preceded by a less then symbol ( < ) and lines from
second file by a greater then symbol ( > ).
A dashed line ( — ) is used to separate output from the two files.
The letters can be used to convert file1 into file2:
c Replace lines from file1 with those from file2.
d Delete lines from file1.
a Add lines from file2 to file1.
The two files have three differences:
Line 2 differs.
Lines 4 and 5 of test2 are not present in test1. These lines would need to
be added after line 3 of test1 for the files to be the same.
Since you normally think in terms of converting the first file into the second file it is better to say that line 5 would need to be deleted from test1 for the files to be the same.
Ignores repeated blanks (e.g. <space><space> is the same as <space>) and blanks at the end of lines.
Ignore all spaces and tabs (e.g. 1 or 2 is equivalent to 1or2).
Ignore case (e.g. howdy, HOWDY and HoWdY are equivalent).
Use the context output format. Context output includes three lines before and after those that are normally printed to give “context” for the differences.
Use the unified output format, easy readable with file information.
Like -c but include n lines of context output.
Produce a script file that can be used by ed to convert file1 to file2. This option is not used as often as the patch command to convert file1 to file2.
Do a faster but less accurate comparison. This does not work well if the files are very different and cannot be used with the -e option.
Directory related Options
Output is formatted so that each file comparison occurs on a new page. Other comparisons are listed on a final page.
Recursively compare all files in common subdirectories.
Include a listing of all identical files in the output.
diff /tmp/oldFolder/ /tmp/newfolder/
Try it out! The output is self-explaining.
Changed files in folder tree
lists all files that have changed in a folder tree
diff -uwrq /tmp/oldFolder/ /tmp/newfolder/
Ignore Case and Repeated Blanks
Report the differences between poem1 and poem2 using the -i option to ignore the differences between upper and lower case charactersand the -b
option which ignores all repeated blanks and blanks at the end of lines.
diff -ib test1 test2
Patch with Diff
The recommended way …
Using output redirection
diff test1 test2 > diff_for_patch
patch test1 diff_for_patch
test1 is converted into test2 regarding the diff results, wich you can
influence by using options.
diff -r dir1 dir2 > dir2.patch
you can create a patch over all files in a directory which you can apply using
cd dir1 ; patch -p1 < dir2.patch
Ed with Diff
The -e option creates a script that gives directives to the ed text editor to convert file1 into file2.
diff -e test1 test2 > diff_for_ed
( cat diff_for_ed && echo w )| ed – test1
( ) for subshells, | – piping, ‘echo w’ is appended to ed input to make ed write the file
Diff with Context and more verbose Output
diff -c test1 test2
You can use the -C n option for n context lines. Output related to file1 is
preceded by stars (***) and file2 by dashes (—). Differences are separated by a long row of stars (***************).
In output the following symbols are used:
! Indicates corresponding lines in the two files that differ.
+ Indicates lines that exist in file2 but not file1.
– Indicates lines that exist in file1 but not file2.
Diff with Script
Using the Bourne shell:
# use -h option for faster, less accurate comparison
diff -h $1 $2 &gt; /dev/null
# Asking for exit status using $? for bourne shell, 0 means equal
if [ $? -eq 0 ]; then
elif [ $? -eq 1 ]; then
echo an error occurred
By: K Rekk