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



DIFF, FIND & MD5SUM
( The difference in 2 Directories )


In the previous Tip I explained about the commands diff and cmp . . . . here is a nice example on how to use diff ( we agreed we would forget about cmp ) in combination with the "md5sum" and "find" command.

There was a question from a forum member about how to make a list of checksums of all files in a directory and those below. Again we make a "test" directory and placed in it some files to create the example.

First we cd to that directory:

CODE
$ cd /home/bruno/test


Then we make the checksums of all the files in that directory using the find command: ( you can see all about the find command Here )

CODE
$ find  .  -type f   2>/dev/null  -exec md5sum {} \; >list1.txt

( Note: the dot just after the command "find" means: current directory and below )
This will make a file called "list1.txt" and this is how it will look like:

QUOTE = list1.txt
f3dd21a2023813be41c9f4b879783d38  ./colors.txt
9dab480789443d080d347138acdad7e5  ./quick_ref.pdf
f91449e7f5661b64c568a6c8a8834224  ./Console-Colors
a1ef1c1dc94d5a6d44dd5e0678f436a8  ./dsp.sxw
37e30a1472eec2d643ff3c68dfec9b91  ./Host_DSL.kwd
8f7a56b9b2a4e5ad703d5771f1194745  ./knoppix.pdf
79619689e2f2427914e7375024a8f3f6  ./New.txt
17b5e1592a475ce4657dd37b614a4b01  ./list1.txt
f37f98c529cfb9cf81fae1cf8eff3726  ./Xsreensaver
4ab309b66eaaf843b8ab3a89abac5a89  ./Prompt.txt
35239c3bf13ac2d6ed68bfcda173cca9  ./Test_Cron
c961818ee81c546ba23ba0f9d025045a  ./Color-Wheel


Now I copy that test directory, make some changes to only one file ( colors.txt ) in that second directory and we run the same command again but write the output this time to "list2.txt"

CODE
$ find  . -type f   2>/dev/null  -exec md5sum {} \; >list2.txt


After that we have 2 files with checksums: list1.txt and list2.txt . . . and we use the "diff" command to see what the difference is and what files in that directory are the same and which files have changed.
This shows us that only the "colors.txt" file is different and all the other files have the same checksum . . . now, we can do a diff on the 2 color.txt files to see where the different checksum comes from:

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


And now we not only see what file in the two directories was different from the other, but also where the difference comes from.




Another approach is this:

CODE
$ find  . -type f   2>/dev/null  -exec md5sum {} \; >test.md5

That will make a "test.md5" file that you can use at a later date to check if anything in that directory has changed:

CODE
$ md5sum -c test.md5

The output will be a list of files present in that directory with "OK" or "FAILED"



Next time the "dirdiff" script from Ragnar that automates what we have done here above





Bruno


-- Apr 19 2005 ( Revised Dec 8 2007 ) --


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