Re: [wlug] Compare 2 directories with each other.

On 1 Aug 2003, Oliver Jones wrote:
Howdy. Anyone know of a command that lets you compare the contents of two different dirs?
[Various suggestions using diff/ls/tree deleted]. I used to use a shell script that would use diff to compare two directories and all their subdirectories. I mainly used it to verify that a backup CD of crucial data had been written correctly. I have since written it as a perl script which is a bit nicer. Note that I have never got around to tidying up this script to use proper arguments, etc. It currently is set to comparing the current directory to the CD. Edit the line "@totals = &dodir(".","/media/cdrom");" if you want to change the directories it uses. Cut out the script from below and save it as whatever you want to call it and remember to set the execute bit on it. #!/usr/bin/perl # sub dodir { local ($dir) = $_[0]; local ($refdir) = $_[1]; local (@totals) = (0,0,0,0); opendir(F,$dir) || die "ERROR: cannot opendir $dir"; foreach $name (sort readdir(F)) { if ($name ne "." && $name ne "..") { $fullname = $dir . "/" . $name; $refname = $refdir . "/" . $name; if (-d $fullname) { printf "%-50s [d] ", $fullname; if (! -d $refname) { print "ERROR - not on CD\n"; $totals[0]++; }else{ print "(entering)\n"; @subtot = dodir($fullname, $refname); $totals[0] = $totals[0] + $subtot[0]; $totals[1] = $totals[1] + $subtot[1]; $totals[2] = $totals[2] + $subtot[2]; $totals[3] = $totals[3] + $subtot[3]; } }elsif (-f $fullname) { printf "%-50s ... ",$fullname; if (! -f $refname) { print "ERROR - not on CD\n"; $totals[1]++; }else{ $retval = system("cmp -s '$refname' '$fullname'"); if ($retval != 0) { print "ERROR - files differ.\n"; $totals[2]++; }else{ print "OK.\n"; } } }else{ printf "%-50s [can't handle]\n",$fullname ; $totals[3]++; } } } closedir(F); @totals; } $ok = 1; @totals = &dodir(".","/media/cdrom"); print "\n"; if ($totals[3] > 0) { if ($totals[3] == 1) { print "WARNING: There was one [reference] file that I couldn't read.\n"; }else{ print "WARNING: There were $totals[3] [reference] files that I couldn't read.\n"; } } if ($totals[0] > 0) { $ok = 0; if ($totals[0] == 1) { print "WARNING: There was one missing directory.\n"; }else{ print "WARNING: There were $totals[0] missing directories.\n"; } } if ($totals[1] > 0) { $ok = 0; if ($totals[1] == 1) { print "WARNING: There was one missing file.\n"; }else{ print "WARNING: There were $totals[1] missing files.\n"; } } if ($totals[2] > 0) { $ok = 0; if ($totals[2] == 1) { print "WARNING: There was one bad file.\n"; }else{ print "WARNING: There were $totals[2] bad files.\n"; } } if ($ok) { print "All OK.\n"; } Cheers Michael
participants (1)
-
Michael Cree