views:

69

answers:

4

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 copy 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.

A: 

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.

ghostdog74
+3  A: 

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

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

MattB
Hey, that does it! But now I have a new problem. My source files are r-- mode (because they came out of source control) and now the dest files become r--, which means next time there are new files to copy over, the copy will fail since the files they try to copy over are not writable. And -f just ignores the failure instead of making it work.
Southern Hospitality
+1  A: 

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)/
Alok
That'll do it, but MattB beat you do the punch. But I have a new problem, see commends on his answer.
Southern Hospitality
Hmm, you can add `chmod -R o+w $(B)` after the `cp` then.
Alok
o+w? I'm feeling rather friendly today, aren't I? maybe u+w? But either way, that'll end up chmodding everything in the destination to be +w including files that I didn't copy, just existed there before. Which is none in this case, but hmm, I'd rather not if I could avoid it.Maybe I'll retreat to a single layer copy (instead of infinitely recursive) which I can implement with globbing and install.
Southern Hospitality
LOL. Yeah, I meant `u+w`, sorry. I meant to write a warning note about the security implications with that comment, sorry.
Alok
A: 

Instead of cp, use rsync

William Pursell