Difference between revisions of "Diff"

From CsWiki
Jump to: navigation, search
(usage)
 
(4 intermediate revisions by 3 users not shown)
Line 3: Line 3:
  
 
==usage==
 
==usage==
   '''echo "Labaneh" > a'''
+
   '''$ echo "Labaneh" > a'''
   '''echo "Labaneh" > b'''
+
   '''$ echo "Labaneh" > b'''
   '''diff a b''' // No output - files are identical.
+
   '''$ diff a b''' // No output - files are identical.
   '''rm a'''
+
   '''$ rm a'''
   '''echo "labaneh" > a''' // Note the difference
+
   '''$ echo "labaneh" > a''' // Note the difference
   '''diff a b'''
+
   '''$ diff a b'''
 
   1c1
 
   1c1
 
   < labaneh
 
   < labaneh
 
   ---
 
   ---
 
   > 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 and variants=
 
==diff frontends==
 
==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''' 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==
 
==diff-ing 3 files==
Line 22: Line 25:
  
 
=Using md5sum as a diff replacement=
 
=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.
+
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.
  
 
==usage==
 
==usage==
Line 50: Line 53:
 
   3763552bf8bd868dec3e321ecc7886ea *a
 
   3763552bf8bd868dec3e321ecc7886ea *a
 
   3763552bf8bd868dec3e321ecc7886ea *b  // Once again, identical.
 
   3763552bf8bd868dec3e321ecc7886ea *b  // Once again, identical.
 
  
 
=External Links=
 
=External Links=
 +
[http://www.computerhope.com/unix/udiff.htm Using diff]
 +
 +
[http://www.gnu.org/software/diffutils/manual/diff.html GNU page about file comparison]
  
 
[http://en.wikipedia.org/wiki/Diff Wikipedia's '''diff''' page]
 
[http://en.wikipedia.org/wiki/Diff Wikipedia's '''diff''' page]
 +
 +
[http://en.wikipedia.org/wiki/Md5sum Wikipedia's '''md5sum''' page]

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.

usage

 $ 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
 1c1
 < 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.

usage

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
 Labaneh
 Humus
 $ 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