tags:

views:

484

answers:

6

I was hoping:

cp -R src/prog.js images/icon.jpg /tmp/package

would yield a symmetrical structure in the destination dir:

/tmp
|
+-- package
    |
    +-- src
    |   |
    |   +-- prog.js
    |
    +-- images
        |
        +-- icon.jpg

but instead, both of the files are copied into /tmp/package. A flat copy.

(This is on OSX).

Is there a simple bash function I can use to copy all files, including files specified by wildcard (e.g. src/*.js) into their rightful place within the destination directory. A bit like "for each file, run mkdir -p `basename $file", but perhaps a single command.

This is a relevant thread, which suggests it's not possible. The author's solution isn't so useful to me though, because I would like to simply provide a list of files, wildcard or not, and have all of them copied to the destination dir. IIRC MS-DOS xcopy does this, but there seems to be no equivalent for cp.

+9  A: 

One way:

tar cf - <files> | (cd /dest; tar xf -)
Randy Proctor
oh, i like this much better than my answer.
EMPraptor
You can also use the `-C` option to do the chdir for you - `tar cf - _files_ | tar -C /dest xf -` or something like that.
D.Shawley
thanks, this is concise, though I prefer rsync for simplicity.
mahemoff
A: 

try...

for f in src/*.js; do cp $f /tmp/package/$f; done

so for what you were doing originally...

for f in `echo "src/prog.js images/icon.jpg"`; do cp $f /tmp/package/$f; done

or

v="src/prog.js images/icon.jpg"; for f in $v; do cp $f /tmp/package/$f; done

edit: correction

edit 2: simplified first example

edit 3: added third example

EMPraptor
+3  A: 

Alternatively, if you're old-school, use cpio:

cd /source;
find . -print | cpio -pvdmB /target

Clearly, you can filter the file list to your heart's content.

The '-p' option is for 'pass-through' mode (as against '-i' for input or '-o' for output). The '-v' is verbose (list the files as they're processed). The '-m' preserves modification times. The '-B' means use 'big blocks' (where big blocks are 5120 bytes instead of 512 bytes); it is possible it has no effect these days.

Jonathan Leffler
+3  A: 

Have you tried using the --parents option? I don't know if OS X supports that, but that works on Linux.

cp --parents src/prog.js images/icon.jpg /tmp/package

If that doesn't work on OS X, try

rsync -R src/prog.js images/icon.jpg /tmp/package

as aif suggested.

ustun
ok so i spoke too soon. i like this answer best.
EMPraptor
thanks. turns out "cp --parents" isn't possible on mac, but it's nice to know the flag for other unixen.rsync -R is the simplest portable solution for this problem.
mahemoff
+2  A: 

rsync of course! tutorial here. and here

Or unison

Aif
+2  A: 

rsync's -R option will do what you expect. It's a very feature-rich file copier. For example:

$ rsync -Rv src/prog.js images/icon.jpg /tmp/package/
images/
images/icon.jpg
src/
src/prog.js

sent 197 bytes  received 76 bytes  546.00 bytes/sec
total size is 0  speedup is 0.00

Sample results:

$ find /tmp/package
/tmp/package
/tmp/package/images
/tmp/package/images/icon.jpg
/tmp/package/src
/tmp/package/src/prog.js
Ryan Bright