Copy everything in a subdirectory across to the relevant subdirectory elsewhere

2

At the moment I'm doing:

for i in mdx/[0-9][0-9].png; do cp $i ../../d_s_c/images/mdx ; done

But as well as the directory mdx, I also have other directories called ntt, etc.

How can I say 'For everything in a subdirectory, copy it across to the same subdirectory over at at ../../d_s_c/images'?

Richard

Posted 2011-09-16T17:30:51.547

Reputation: 605

Bonus points for 'copy if file has changed or does not exist in destination, leave otherwise'. – Richard – 2011-09-16T17:31:19.410

Answers

1

If you only want to copy things that have changed or are new, you'd be better off using rsync. To cover your other subdirectories, presuming you don't want everything.

MYLIST="mdx ntt etc"
for j in $MYLIST
do
        rsync -aq --include='[0-9][0-9].png' --exclude='*' ./$j/ ../../d_s_c/images/$j/ ; 
done

If you want to see what it's doing, you can substitute v for q in the parameters. I think this should work for you, you may have to make some adjustments for your environment.

OldWolf

Posted 2011-09-16T17:30:51.547

Reputation: 2 293

This worked brilliantly, and I've learned something today, thank you! – Richard – 2011-09-16T21:18:19.777