0

I have 2 directories DIR1 and DIR2

DIR1 is base directory, where new changes come often, sometimes new sub-directories as well. DIR2 has older version of files.

I want to compare these 2 directories, such that if any of the subdirectory of DIR1 has any file which is new/different than the respective subdirectory of DIR2, it should copy entire respective subdirectory to DIR3.

Hence DIR3 should have all the subdirectories which have been identified with the change.

0
 for dir in $(find DIR1 -type d) ;do
   newdir=$(echo ${dir} | sed -e "1,1s/^DIR1/DIR3/")
   mkdir -p ${newdir}
 done

This block above creates all directories in DIR1 under DIR3. If you are creating new directories under DIR1, which are not in DIR2, you should run this snippet when there are new directories under DIR1, otherwise, running this once will be enough

Then:

for file in $(find DIR1 -type f) ;do
  oldfile=$(echo ${file} | sed -e "1,1s/^DIR1/DIR2/")
  if [ -f ${oldfile} ]
  then
    diff ${file} ${oldfile} >/dev/null; r=${?}
    if [ $r -ne 0 ]
    then
      newfile=$(echo ${file} | sed -e "1,1s/^DIR1/DIR3/")
      cp ${file} ${newfile}
    fi
  else
    newfile=$(echo ${file} | sed -e "1,1s/^DIR1/DIR3/")
    cp ${file} ${newfile}
  fi
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.