Previous Table of Contents Next


If both arguments are directories, diff sorts the contents of each directory, then performs normal diff comparisons on files. Binary files that differ, common subdirectories, and files residing in only one directory are displayed.


The output of diff varies depending on the options you specify. The n represents line numbers and line represents the text on a line. The greater than and less than symbols are used to distinguish which lines belong in which file. For example,

                    < Always a line from file1.
                    > Always a line from file2.

shows that the first line resides in file1, the -- separates the two files' output, and the third line resides in file2.


The append string informs you which strings exist in file2 but not in file1. Therefore, the lines in file2 must be appended after the displayed line in file1.

                    n1an2,n3 <line n2
                    <line n3

The first line informs you to append lines n2 through n3 from file2 after line n1 in file1. The lines of text following the < are to be copied from file2 and placed in file1 after line n1.


The change string informs you which strings in file1 must be changed to make the two files agree. The change is basically a delete and append command. It replaces entire lines in file1 with entire lines from file2.

n1,n2cn3,n4 Informs you to replace lines n1 through n2 in file1 with
<line n1 lines n3 through n4 from file2.
<line n2
>line n3
>line n4


The delete string informs you which strings do not exist in file2 but do exist in file1. The lines in file1 must be deleted to make the files conform.

n1,n2dn3 Informs you to delete lines n1 through n2 in file1. File1
<line n1 is identical to file2 up to line n3 in file2.
<line n2


The following steps create an original file and version control files for each updated version of the original. You will only need to keep the original file and a version file for each version.

You must first create your original file.

                    cj> vi original

When you want to update the original you simply make a copy and edit the new file (named new). Make your changes to the new file and save it. Then execute diff to make a version control file. The commands are:

                    cj> cp original new
                      vi new
                      diff -e original new > v01
                      rm new

Now you have the original and the control file for version 1.

To create a new version as you need to you can write a script named vs. The commands for vs are:

                    cj> cat vs
                    (shift ; cat $1 ; echo '1,$p' ) | ed -s$1 > new
                    vi new
                    diff -e $1 new > $3
                    rm -f new

On BSD and SV prior to R4 use a - instead of -s on the ed command.

To create a new version (version 2) simply use your vs script,

                    cj> vs original v01 v02

To make a copy of an existing version you simply execute the following diff command.

                    cj> diff -e original vXX > vXX.out


The output scripts produced by the -e and -f options may contain periods (.), which cause the ed editor to change to command mode. If diff meant for the . to be part of the text, the changed file will not be correct.


Refer to the cmp and comm commands described in modules 20 and 22.


The diff command reads input from two files. The hyphen (-) may be used on the command line to represent the standard input, thus allowing input to be from a pipe, indirection, or your keyboard.


The following returned codes are returned by diff. You may want to use these codes in shell scripts to verify two files are identical.

0 No differences found.
1 Differences where found.
2 Errors occurred during execution.


The basic use of diff is to find text differences between two files. You can specify the -e option to generate usable ed scripts. Then you can invoke ed to make the necessary changes to the appropriate file.

The diff command proves to be useful in comparing versions of files. You may find it helpful in comparing versions of a letter or program source file.


In this activity you use the diff command to compare two small files. Begin at the shell prompt.

1.  Type diff file1 file2 and press Return. Notice the output strings that inform you how to change file1 to make it match file2.
                    cj> diff file1 file2

All the lines of file2 would have to be added to file1 to make file1 identical to file2.
2.  Turn to Module 34 (SV), Module 27 (BSD) to continue the learning sequence.

Previous Table of Contents Next