开发者

How do I use cp to copy one directory to new name repeatedly and correctly?

I am writing a makefile. However except for the path expansion and restrictions on what I can do, this is basically a shell scripting question. As part of the make process, I want to cop开发者_开发技巧y a directory to the destination. My source directory and destination directory are variables, so I can't assume a lot about them. They may be fully qualified, they may be relative.

But I want to copy the directory $(A) to the destination $(B). $(B) is the name I want the directory to have at the destination, not the directory I want to copy $(A) to (i.e. the resulting directory name will be $(B), not $(A)/$(B)), it might be the same path for source and dest, so I check with an ifneq ($(A),$(B)) before doing anything. But the question is, what do I do in the block.

If I do

cp -r $(A) $(B)

it will work the first time. $(A) is copied to $(B). But if the rule triggers again later, it will make a new copy of $(A) inside $(B) (i.e. $(B)/$(A)).

And before you ask, I'd rather not rm -r $(B) before doing this if at all possible.


cp -r $(A)/ $(B)

Adding the slash will copy the contents of $(A) into $(B), and create $(B) if it does not exist.


If you're using GNU cp, you can do:

mkdir -p $(B)
cp -a --target-directory=$(B) $(A)

You can also try rsync:

rsync -a $(A) $(B)/


how about using cp -r $(A) $(B) for the first time, then use -u of copy to copy only when source is newer than destination?? see man page of cp for more.


Instead of cp, use rsync

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜