Chksum on two directories then copy if they are not identical or existing

How can I have an intelligent script that will copy from source to destination directory if the file doesnt exist there or the chksum is not match.

SOURCE directory: 

 for i in `ls`
> do
> echo $i
> md5sum $i
> echo ""
> done
asdasda
00039a616135792fb609d04cf27aed95  asdasda

chksums
5053d1886cbb9a9c5e9399bbc187d030  chksums

zunauthftp.vf6
00039a616135792fb609d04cf27aed95  zunauthftp.vf6

zunauthimap.vf6
6b1815b8f75b651916549dafb5fa347c  zunauthimap.vf6

zunauthpop.vf6
8589e73b814c1241b1c2c887340f711a  zunauthpop.vf6

zunauth.vf6
5590c0992a894493ca126123aa958a2e  zunauth.vf6

zunauweb.vf6
64c5ca1dc4582da68818adae80507c8c  zunauweb.vf6

Destination:

for i in `ls`
> do
> echo $i
> md5sum $i
> echo ""
> done
chksums
5053d1886cbb9a9c5e9399bbc187d030  chksums

zunauthftp.vf6
00039a616135792fb609d04cf27aed95  zunauthftp.vf6

zunauthimap.vf6
6b1815b8f75b651916549dafb5fa347c  zunauthimap.vf6

zunauthpop.vf6
8589e73b814c1241b1c2c887340f711a  zunauthpop.vf6

zunauth.vf6
5590c0992a894493ca126123aa958a2e  zunauth.vf6

zunauweb.vf6
125993721bfdcc9f8de614a182da0a01  zunauweb.vf6
In this case, file asdasda(not existing) and zunauweb.vf6(different chksum) should be copied to the destination directory.

Try (not thouroughly tested, so double check!), given SRCPATH and DSTPATH hold the respective directories, and diff offers the options needed:

cd $DSTPATH
md5sum * > /tmp/dstsum
cd $SRCPATH
md5sum * > /tmp/srcsum
diff -y -b --suppress-common-lines /tmp/srcsum /tmp/dstsum | awk -vdstpath="$DESTPATH" '{print "echo cp", $2, dstpath "/" $2}' | sh

It should print out what it would do in case. When happy with the result, remove the echo command.

1 Like

It is also worth looking a rsync which has these features and more:

$ rsync -ac --dry-run $SRCPATH/ $DSTPATH

-a archive mode (see manual, basically recursive whole directory)
-c skip based on checksum, not mod-time & size

remove --dry-run above if everything looks OK to you

2 Likes

After running diff.
thanks

cp: cannot stat �aa': No such file or directory
cp: cannot stat �asdasda': No such file or directory
cp: cannot create regular file �/zunauweb.vf6': Permission denied

---------- Post updated at 07:23 AM ---------- Previous update was at 07:19 AM ----------

diff -y -b --suppress-common-lines /tmp/srcsum /tmp/dstsum | awk -vdstpath="$DESTPATH" '{print "echo cp", $2, dstpath "/" $2}' | sh

IN that command, where is the sourcepath

diff -y -b --suppress-common-lines /tmp/src1 /tmp/dest1
3cdbca7c78ccd11f9314e4bf0da3b20d  aa                          <
00039a616135792fb609d04cf27aed95  asdasda                     <
64c5ca1dc4582da68818adae80507c8c  zunauweb.vf6                | 125993721bfdcc9f8de614a182da0a01  zunauweb.vf6

cat aa
#cd $DSTPATH
cd /tmp/rac
md5sum * > /tmp/src1
#cd $SRCPATH
cd  /tmp/rac1
md5sum * > /tmp/dest1
diff -y -b --suppress-common-lines /tmp/src1 /tmp/dest1 | awk -vdstpath="$DESTPATH" '{print "cp", $2, /tmp/rac1 "/" $2}' | sh

---------- Post updated at 01:05 PM ---------- Previous update was at 07:23 AM ----------

I figured it out. Thank you all.

Figured out what? How did you fix it?

Looking at it again, I think the last "/" $2 can be dropped...