Compare directories and copy differences (files) in a another directory

Hey
im working on script that can compare 2 directory and check difference, then copy difference files in third diretory.
here is the story:
in folder one we have 12 subfolder and in each of them near 500 images hosted.
01 02 03 04 05 06 07 08 09 10 11 12
in folder 2 we have same subfolder but maybe diffrence images like in folder 2, subfolder 07 maybe image 45 is deleted so we need backup of it,
ive write this script:

f1="" 
 f2=""
 des=""
for x in `rsync -rcnC --out-format="%n"  $f1 $f2`
do
        if [ -d "$f1/$x" ];
then
mkdir -p "$des/$x"
else
            cp -frv $f1/$x $des/$x
        fi
done
  

also i try to use diff command but script eat ram and cpu. im looking for alternative method, is there any ?

What will a "difference" be to you? Just files missing, i.e. different directory contents? Or files with different meta data, i.e. size and/or timestamp? Or do you need to byte compare every single file to its counterpart?

If the file names are the basis of comparison:

diff -r dir1 dir2 | grep dir1 | awk '{print $4}' > ~/mydiff

diff -r does a recursive comparison of directories and their subdirectories

2 Likes