Difference between revisions of "Diff"

From CsWiki
Jump to: navigation, search
(diff frontends)
Line 53: Line 53:
   3763552bf8bd868dec3e321ecc7886ea *a
   3763552bf8bd868dec3e321ecc7886ea *a
   3763552bf8bd868dec3e321ecc7886ea *b  // Once again, identical.
   3763552bf8bd868dec3e321ecc7886ea *b  // Once again, identical.
=External Links=
=External Links=

Latest revision as of 15:00, 2 August 2018

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

useful flags

Reasonably useful flags are -i (ignores case differences) and -w (ignores all spaces and tabs). Read the man page for more details.

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 meld.

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 a one-line hash code instead of a 20 megabytes file. The obvious disadvantage of md5sum is its blindness to the exact location or nature of the difference; the command can only tell if the files are identical or not. md5sum 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

Using diff

GNU page about file comparison

Wikipedia's diff page

Wikipedia's md5sum page