Thursday, June 23, 2011

Script to compare and Print Difference between two Directories recursively


#!/bin/bash

# cmp_dir - program to compare two directories and subDirectiories
# Manish
#Please remove Echo statements i had added them for debugging
# this is the Function part of it
# Expects two argument as Source directory and target Directory now bachalo is folder which does not exhist in destination
# so it says folder does not exhist - just copy  but test is in source and dest so it gets in to that
#**********************************

searchF()
{
file=$1
args=$2
  #echo "in function Search"
# Process each file in directory_1, comparing it to directory_2

for filename in $file/*; do
    fn=$(basename "$filename")
arg="$args/$fn"
   
#echo "Hellooooo"$fn
   
if [ -f "$filename" ]; then # Checking the File and Comparing the Diff
        if [ ! -f "$arg" ]; then
            echo "$fn is missing from $arg"
            let "missing+=1"
#sleep 1
#echo $missing "is the current missing count"
        fi
    elif [ -d "$filename" ]; then # i need to make it recursive here for directories inside-- i think
#echo "HelloBidu"$filename
arg= "$2/$fn"
let "missing+=1"
#sleep 1
#echo $missing "is the current missing count"
searchF $filename $arg #recusrion
    fi
done

}
#****************************
# Main Block Starts Here
# Check for required arguments

missing=0
[ $# -ne 2 ] &&  echo "usage: $0 directory_1 directory_2" 1>&2 &&
    echo "usage: $0 directory_1 directory_2" 1>&2
   
fi

# Make sure both arguments are directories
if [ ! -d $1 ]; then
    echo "$1 is not a directory!" 1>&2
    exit 1
fi

if [ ! -d $2 ]; then
    echo "$2 is not a directory!" 1>&2
    exit 1
fi

#function call
searchF $1 $2
echo "$missing files missing"

No comments:

Post a Comment