Bash: 递归复制命名文件,保留文件夹结构

我希望:

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

将在目标 dir 中产生一个对称的结构:

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

但是,这两个文件都被复制到/tmp/package 中。

有没有一个简单的 bash 函数可以用来复制所有文件,包括通配符指定的文件(例如 src/*)。Js)放置到目标目录中的合适位置。有点像“对于每个文件,运行 mkdir -p $(dirname "$file"); cp "$file" $(dirname "$file")”,但可能只有一个命令。

这是一个相关的线索,说明这是不可能的。不过,作者的解决方案对我来说并没有多大用处,因为我想简单地提供一个文件列表,不管有没有通配符,然后将它们全部复制到目标目录中。IIRC MS-DOS xcopy 可以做到这一点,但似乎没有等效的 cp。

74758 次浏览

One way:

tar cf - <files> | (cd /dest; tar xf -)

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

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.

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.

rsync of course! tutorial here. and here

Or unison

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