This is a tutorial I wrote years ago. Now included here. Hope it can still be useful.
Content:
Simple Example
Options
Directory related Options
Resources
Examples
Comparing Directories
Changed files in folder tree
Ignore Case and Repeated Blanks
Patch with Diff
Ed with Diff
Diff with Context and more verbose Output
Diff with Script
Purpose
diff reports the differences between two files.
Description
diff [options] <i>file1</i> <i>file2</i>
File test1:
Test! A common line. Really common line. One more common line. Only here.
File test2:
Test! A almost common line. Really common line. Really not common line. One further line. One more common line.
For example
$ diff test1 test2
will output:
2c2 < A common line. --- > A almost common line. 3a4,5 > Really not common line. > One further line. 5d6 < 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.
Useful Options
|
Option |
Description |
|
-b |
Ignores repeated blanks (e.g. <space><space> is the same as <space>) and blanks at the end of lines. |
|
-w |
Ignore all spaces and tabs (e.g. 1 or 2 is equivalent to 1or2). |
|
-i |
Ignore case (e.g. howdy, HOWDY and HoWdY are equivalent). |
|
-c |
Use the context output format. Context output includes three lines before and after those that are normally printed to give “context” for the differences. |
|
-u |
Use the unified output format, easy readable with file information. |
|
-C n |
Like |
|
-e |
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. |
|
-h |
Do a faster but less accurate comparison. This does not work well if the files are very different and cannot be used with the |
Directory related Options
|
Option |
Description |
|
-l |
Output is formatted so that each file comparison occurs on a new page. Other comparisons are listed on a final page. |
|
-r |
Recursively compare all files in common subdirectories. |
|
-s |
Include a listing of all identical files in the output. |
Examples
Comparing Directories
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.
With
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:
#!/bin/sh
# 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
echo identical
elif [ $? -eq 1 ]; then
echo different
else
echo an error occurred
fi