tags:

views:

302

answers:

1

Basically I'm trying to copy the contents of both dir1 and dir2 (excluding subdirectories) into dir3. The caveat is that if a file exists in both dir1 and dir2, I need to copy the newer file. Lets say the newer file exists in dir2.

I had:

find dir1 -type f -exec cp {} dir3 \;
find dir2 -type f -exec cp -u {} dir3 \;

Doing it this way leads to this problem: since the files from dir1 are copied before dir2, all files from dir1 (that are now in dir3) are considered newer, and won't be overwritten.

I'm thinking that you have to process a file in dir1, check if it exists in dir2, and then check which is newer. However I'm not sure how to do this, besides that you could use"-nt". I'm thinking that I am just going about this the wrong way.

+3  A: 
cp -vfudp dir1/* dir3/
cp -vfudp dir2/* dir3/
Don
ah, so -p will keep the timestamp of what it was in dir1 correct?
kevin
yes, the p for preserve will retain a bunch of stuff, see: man cp
Don