Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown   Email 



DIFF & CMP
( Find the difference )


Comparing two files to see if they are identical is not always easy using a GUI text-editor and having them side by side. But of course Linux has a command for that to make things easier. Actually there are two commands: "diff" and "cmp" but you will see that the first one is so much easier to use then the second one.

Okay let us set up an example to explain things . . . I place two almost identical files ( colors1.txt and colors2.txt ) in a "test" directory.

First we cd to that directory:

CODE
$ cd /home/bruno/test


Now first I will show you the use of diff without any options:

CODE
$ diff colors1.txt colors2.txt

 6c6
 < # indigo
  ---
  > # indigo-blue
  11d10
  < # sienna
  12a12
  > # darkblue
  15c15
  < # tomato
  ---
  > # tomato-red



We have to admit that the output is at least confusing . . . so now we add the -u ( unified ) option:

CODE
$ diff -u colors1.txt colors2.txt

  --- colors1.txt 2005-04-01 20:45:21.954828407 +0200
  +++ colors2.txt 2005-04-01 20:44:50.152742422 +0200
  @@ -3,13 +3,13 @@
  # darkred
  # deeppink
  # firebrick
  -# indigo
  +# indigo-blue
  # limegreen
  # royalblue
  # sandybrown
  # seagreen
  -# sienna
  # silver
  +# darkblue
  # skyblue
  # teal
  -# tomato
  +# tomato-red



Now we start to see clearly . . . . . the differences from color1.txt to color2.txt are marked with "-" and "+" . . . . In the first file it says "indigo" in the second one "indigo-blue" the second one has no sienna but has the darkblue the first one has not . . etc. etc.

Okay now we will make it even better to read:

CODE
$ diff -y -W 70 colors1.txt colors2.txt

# chocolate            
# crimson
# darkred
# deeppink
# firebrick
# indigo                                    |
# limegreen
# royalblue
# sandybrown
# seagreen
# sienna                                 <
# silver
                                               >
# skyblue
# teal
# tomato                                  | 
                                                                                                             
# chocolate            
# crimson
# darkred
# deeppink
# firebrick
# indigo-blue
# limegreen
# royalblue
# sandybrown
# seagreen

# silver
# darkblue
# skyblue
# teal
# tomato-red


See this ?? . . . . The "|" indicates there is a difference and the ">" and  "<" tells us what is left out or added !! The -y option stands for "Side by side" . . and the -W 70 sets the width for the column.



Now we have found the best way to compare the two files I will show you the other command: "cmp" The cmp command is a whole other story. Let me first give an example:

CODE
$ cmp colors1.txt colors2.txt

 colors1.txt colors2.txt differ: byte 64, line 6



That is all the output it gives . . . that is because cmp stops at the first byte that differs and then exits, writing the line and the byte that causes it to stop. Not really a clear output is it ?

Sure we can give cmp the -l option so it does not stop at the first byte that differs but give all the bytes that are different . . but then you will see that ALL the bytes after byte 64 are different and you will only get a real long list even from the short example we use:

CODE
$ cmp -l colors1.txt colors2.txt

 64  12  55
 65  43 142
 66  40 154
 67 154 165
 68 151 145
 69 155  12
 70 145  43
 71 147  40
 72 162 154
 73 145 151
 74 145 155
  <<---------------------
  I did remove byte 75 to 140 to shorten it a bit
  ---------------------->>
  141  43 153
  142  40 171
  143 164 142
  144 145 154
  145 141 165
  146 154 145
  151 157 145
  152 155 141
  153 141 154
  154 164  12
  155 157  43
  156  12  40
  cmp: EOF on colors1.txt



So it will be clear to you that I prefer using diff . . . . . and the next time we will be using it together with another command we already know to check out the differences of the content of 2 directories. In the 3rd episode on diff I will post a script Ragnar sent me a while ago : "dirdiff" . . . . see you then !



Bruno


-- Apr 12 2005 ( Revised Dec 10 2005 ) --


Tips Linux Explorers   All Things Linux Forum   Great Linux Links   LinuxClues.com   Hometown   Email