views:

110

answers:

3

I'm currently using the usual technique in my Makefile to install individual files:

install:
    install -D executable ${BIN_DIR}

But I just ran across a situation where I need to move a whole directory and all files underneath it into place.

Is cp -r the best way or is there a more linux-y/unix-y way to do this?

+2  A: 

Yeah, it's hard to think of a more unix-ish way that cp -r, although the -r is a relatively late addition to cp. I can tell you the way we used to do it, and that works neatly across filesystems and such:

Let src be the source directory you want to move, and /path/to/target be an absolute path to the target. Then you can use:

$ tar cf - src | (cd /path/to/target; tar xf -)
Charlie Martin
Spot the old-schooler, prepending dashes to tar(1) flags :) I still use this method a lot.But you *definitely* want a ) in your subshell. If /path/to/target doesn't exist, you coud get some... interesting... results.
Martin Carpenter
Charlie Martin
sascha
Oh, I agree. I just don't think the bourne shell did that when I learned this idiom, back in the days of stone knives.
Charlie Martin
+1  A: 

My version of install(1) (Debian) has:

   -d, --directory
          treat all arguments as directory names; create all components of the specified directories

   -t, --target-directory=DIRECTORY
          copy all SOURCE arguments into DIRECTORY

So if you wanted to use install(1) consistently throughout your Makefile you could do:

install -d destdir
install srcdir/* -t destdir

-t isn't recursive however - if srcdir contains directories, then they won't get copied.

Martin Carpenter
thats going to be very dependent on the version of install that is on the target system, it would be suitable if installing to a known OS (such as debian) but if a tgz is the distribution mechanism I'd assume he's trying to be as cross platform as possible.
sascha
A: 

Linking is another viable alternative. That would allow you to keep multiple directories (representing different versions) accessible.

joel.neely
Unfortunately I can't assume that they will keep the extracted tar.gz around after they've done the install so the whole thing needs moved.
Lolindrath
you could consider hardlinking (which would get around the deleting the original files problem) however if the target is on a different file system/partition than the source the hardlink would fail.
sascha