bash file directory diff (1)

Can you give me a script to compare all files in directory A with files in directory B?

I need a bash script that will diff all files in directory A that are also in directory B and put the differences in files in directory C. I also need it to tell me if any files were added to or deleted from directory A relative to directory B. Thanks, you're a dear

#!/bin/bash

if [ -z "$1" -o -z "$2" -o -z "$3" ]
then
		echo 1>&2
		echo "invalid # arguments" 1>&2
		echo "usage ./compare sourcedir targetdir" 1>&2
		echo 1>&2
		exit
fi
sourcedir="$1"
targetdir="$2"
diffdir="$3"

if [ ! -e "$diffdir" ]
then
 mkdir "$diffdir"
fi
if [ ! -e "$diffdir" ]
then
 echo "Could not create $diffdir" 1>&2
fi


# iterate over contents of first directory
find "$sourcedir" -maxdepth 1 -name "*" | while read fn;
do
 # filter on files only (i.e., not directories)
	if [ ! -d "$fn" ]
 then
 		echo "testing $fn" 1>&2
 		# split into filename and path 
 		fpart=${fn##*/}
 		ppart=${fn%/*}

			# construct target name + path
			target="${targetdir}/$fpart"
			
			# see if target exists. if not it was ADDED to the first directory
			if [ ! -e "$target" ]
			then
			 echo "ADDED $target"
			else
			 # compare
			 echo "COMPARE $fn TO $target"
			 difftarget=${diffdir}/${fpart}.diff
			 diff "$fn" "$target" > "$difftarget"
			fi
			
 fi
done
# iterate over the contents of the second directory
# emit any files found here that are not in the first. These are DELETED from dir 1
find "$targetdir" -maxdepth 1 -name "*" | while read fn;
do
 # filter on files only (i.e., not directories)
	if [ ! -d "$fn" ]; then
 		echo "testing $fn"
 		# split into filename and path 
 		fpart=${fn##*/}
 		ppart=${fn%/*}

			# construct source name + path
			target=${sourcedir}/$fpart
			
			# see if target exists. if not it was DELETED to the first directory
			if [ ! -f "$target" ]; then
			 echo "DELETED $target"
			fi
			
 fi
done

Comments

Add a Comment



NOTE: Comments are moderated and will not show until approved.