开发者

BASH script to compare two directories & copy contents into a third directory?

Basically I'm trying to copy the contents of b开发者_StackOverflowoth 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.


cp -vfudp dir1/* dir3/
cp -vfudp dir2/* dir3/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜