From CsWiki
Revision as of 18:04, 30 November 2006 by Adamatan (Talk | contribs)

Jump to: navigation, search

General description of diff

diff is a command line utility that compares two sources (usually files) and displays the differences. In CS courses context, diff is often used to compare the output the student program with the output of the school solution, both provided with the same input, for debugging purposes.


 echo "Labaneh" > a
 echo "Labaneh" > b
 diff a b // No output - files are identical.
 rm a
 echo "labaneh" > a // Note the difference
 diff a b
 < labaneh
 > Labaneh

diff frontends and variants

diff frontends

diff has some GUI frontends. The frontends are most useful trying to understand the specific whereabouts and nature of a textual diff output the yielded differences; The differences are nicely highlighted for the user, including whitespaces. Common frontends are tkdiff and xxdiff.

diff-ing 3 files

diff3 is used for comparing 3 files.

Using md5sum as a diff replacement

md5sum is a common shell utility that gives the md5-hash of a file. Skipping some interesting details, files with the same hash code are completely identical. Since comparing large files stored in different computers is slightly impractical due to the time needed to transfer the files, md5sum is often used to create a md5-hash of a file in each computer, than comparing them. This technique is also used to assure that files are identical to the bit, as diff may be configured to skip white spaces.


Creating a file, checking its md5sum:

 $ echo "Labaneh" > a
 $ md5sum a
 b83c24f2d3cf387845cb7fde205a3a87 *a
 $ echo "Labaneh" > b
 $ md5sum b
 b83c24f2d3cf387845cb7fde205a3a87 *b

Checking the md5 has of two files:

 $ md5sum a b
 b83c24f2d3cf387845cb7fde205a3a87 *a
 b83c24f2d3cf387845cb7fde205a3a87 *b  // Identical hash codes, Identical files

Applying changes to the files:

 $ echo "Humus" >> a
 $ cat a
 $ md5sum a b
 3763552bf8bd868dec3e321ecc7886ea *a
 b83c24f2d3cf387845cb7fde205a3a87 *b  // Note the difference!
 $ echo "Humus" >> b
 $ md5sum a b
 3763552bf8bd868dec3e321ecc7886ea *a
 3763552bf8bd868dec3e321ecc7886ea *b  // Once again, identical.

External Links

Wikipedia's diff page