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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1650164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:38:21  来源:igfitidea点击:

Bash: Copy named files recursively, preserving folder structure

bashshell

提问by mahemoff

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

但是,这两个文件都被复制到 /tmp/package 中。平面副本。(这是在 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 $(dirname "$file"); cp "$file" $(dirname "$file")", but perhaps a single command.

是否有一个简单的 bash 函数可以用来将所有文件(包括通配符(例如 src/*.js)指定的文件)复制到目标目录中的正确位置。有点像“对于每个文件,运行mkdir -p $(dirname "$file"); cp "$file" $(dirname "$file")”,但也许是一个命令。

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.

这是一个相关的线程,这表明这是不可能的。不过,作者的解决方案对我来说并不是那么有用,因为我只想提供一个文件列表,无论是否通配符,并将它们全部复制到目标目录。IIRC MS-DOS xcopy 执行此操作,但似乎没有 cp 的等效项。

回答by ustun

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

您是否尝试过使用 --parents 选项?我不知道 OS X 是否支持它,但它适用于 Linux。

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

If that doesn't work on OS X, try

如果这在 OS X 上不起作用,请尝试

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

as aif suggested.

正如 aif 所建议的那样。

回答by Randy Proctor

One way:

单程:

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

回答by Jonathan Leffler

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

或者,如果您是老派,请使用 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.

'-p' 选项用于 'pass-through' 模式(与 '-i' 用于输入或 '-o' 用于输出)。'-v' 是冗长的(列出处理过的文件)。'-m' 保留修改时间。“-B”表示使用“大块”(其中大块是 5120 字节而不是 512 字节);这几天可能没有效果。

回答by Ryan Bright

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

rsync 的 -R 选项将满足您的期望。这是一个功能非常丰富的文件复制器。例如:

$ 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

回答by Aif

rsync of course! tutorial here.and here

当然是rsync! 教程在这里。和这里

Or unison

齐声

回答by EMPraptor

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