Comments are available to members only. Sign up or Log in to view these comments.
Main Topics
Browse All TopicsI am in need of a script to run on IBM AIX v4.3.3 to compare a directory that contains nearly 60,000 files to a partial copy of the same to determine which files are missing and copy the missing files to the destination. I have done some searching on the web and came up with nothing that I have not thought of already. DIFF is no good because it wants to compare the files. This is not what we are interested in resolving. We only want missing files to be copied to the destination directory from the original source. I found one script that was close called 'cmptree' but cannot figure out how to take its results to the next level to initiate and complete the file copy. The cmptree script is as follows:
#!/bin/bash
#
# cmptree: compare directory trees recursively and report the differences.
# Author: Ives Aerts
function gettype () {
if [ -L $1 ]; then
echo "softlink"
elif [ -f $1 ]; then
echo "file"
elif [ -d $1 ]; then
echo "directory"
else
echo "unknown"
fi
}
function exists () {
if [ -e $1 -o -L $1 ]; then
return 0;
else
echo "$1 does not exist."
return 1;
fi
}
function comparefile () {
cmp -s $1 $2
if [ $? -gt 0 ]; then
echo "$1 different from $2"
# else
# echo "$1 same as $2"
fi
return
}
function comparedirectory () {
local result=0
for i in `(ls -A $1 && ls -A $2) | sort | uniq`; do
compare $1/$i $2/$i || result=1
done
return $result
}
function comparesoftlink () {
local dest1=`ls -l $1 | awk '{ print $11 }'`
local dest2=`ls -l $2 | awk '{ print $11 }'`
if [ $dest1 = $dest2 ]; then
return 0
else
echo "different link targets $1 -> $dest1, $2 -> $dest2"
return 1
fi
}
# compare a file, directory, or softlink
function compare () {
(exists $1 && exists $2) || return 1;
local type1=$(gettype $1)
local type2=$(gettype $2)
if [ $type1 = $type2 ]; then
case $type1 in
file)
comparefile $1 $2
;;
directory)
comparedirectory $1 $2
;;
softlink)
comparesoftlink $1 $2
;;
*)
echo "$1 of unknown type"
false
;;
esac
else
echo "type mismatch: $type1 ($1) and $type2 ($2)."
false
fi
return
}
if [ 2 -ne $# ]; then
cat << EOU
Usage: $0 dir1 dir2
Compare directory trees:
files are binary compared (cmp)
directories are checked for identical content
soft links are checked for identical targets
EOU
exit 10
fi
compare $1 $2
exit $?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: gripePosted on 2005-01-06 at 17:02:03ID: 12978642
Comments are available to members only. Sign up or Log in to view these comments.