Linux 如何使用 gnu cp 命令将文件复制到多个目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/195655/
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-08-03 16:36:09  来源:igfitidea点击:

How to copy a file to multiple directories using the gnu cp command

linuxbashshell

提问by Tom Feiner

Is it possible to copy a single file to multiple directories using the cp command ?

是否可以使用 cp 命令将单个文件复制到多个目录?

I tried the following , which did not work:

我尝试了以下方法,但没有用:

cp file1 /foo/ /bar/
cp file1 {/foo/,/bar}

I know it's possible using a for loop, or find. But is it possible using the gnu cp command?

我知道可以使用 for 循环或 find。但是可以使用 gnu cp 命令吗?

采纳答案by moonshadow

No, cpcan copy multiple sources but will only copy to a single destination. You need to arrange to invoke cpmultiple times - once per destination - for what you want to do; using, as you say, a loop or some other tool.

不可以,cp可以复制多个源,但只能复制到一个目标。您需要安排cp多次调用- 每个目的地一次 - 为您想要做的事情;正如你所说,使用循环或其他一些工具。

回答by Robert Gamble

You can't do this with cpalone but you can combine cpwith xargs:

你不能cp单独做到这一点,但你可以结合cp使用xargs

echo dir1 dir2 dir3 | xargs -n 1 cp file1

Will copy file1to dir1, dir2, and dir3. xargswill call cp3 times to do this, see the man page for xargsfor details.

将复制file1dir1dir2dir3xargs将调用cp3 次来执行此操作,xargs有关详细信息,请参阅手册页。

回答by Robert Gamble

Another way is to use cat and tee as follows:

另一种方法是使用 cat 和 tee 如下:

cat <source file> | tee <destination file 1> | tee <destination file 2> [...] > <last destination file>

I think this would be pretty inefficient though, since the job would be split among several processes (one per destination) and the hard drive would be writing several files at once over different parts of the platter. However if you wanted to write a file out to several different drives, this method would probably be pretty efficient (as all copies could happen concurrently).

我认为这将是非常低效的,因为这项工作将被分成几个进程(每个目的地一个)并且硬盘驱动器将在盘片的不同部分一次写入多个文件。但是,如果您想将一个文件写入多个不同的驱动器,这种方法可能会非常有效(因为所有副本可以同时发生)。

回答by Paul

Wildcards also work with Roberts code

通配符也适用于罗伯茨代码

echo ./fs*/* | xargs -n 1 cp test 

回答by Evgeny

As far as I can see it you can use the following:

据我所知,您可以使用以下内容:

ls | xargs -n 1 cp -i file.dat

The -ioption of cpcommand means that you will be asked whether to overwrite a file in the current directory with the file.dat. Though it is not a completely automatic solution it worked out for me.

命令的-i选项cp意味着您将被问及是否用扩展名覆盖当前目录中的文件file.dat。虽然它不是一个完全自动的解决方案,但它对我有用。

回答by deterb

I would use catand teebased on the answers I saw at https://superuser.com/questions/32630/parallel-file-copy-from-single-source-to-multiple-targetsinstead of cp.

我会使用cattee基于我在https://superuser.com/questions/32630/parallel-file-copy-from-single-source-to-multiple-targets而不是cp.

For example:

例如:

cat inputfile | tee outfile1 outfile2 > /dev/null

回答by Hedgehog

This will copy to the immediate sub-directories, if you want to go deeper, adjust the -maxdepthparameter.

这将复制到直接子目录,如果您想更深入,请调整-maxdepth参数。

find . -mindepth 1 -maxdepth 1 -type d| xargs -n 1 cp -i index.html

If you don't want to copy to all directories, hopefully you can filter the directories you are not interested in. Example copying to all folders starting with a

如果你不想复制到所有目录,希望你可以过滤你不感兴趣的目录。 复制到所有以开头的文件夹的示例 a

find . -mindepth 1 -maxdepth 1 -type d| grep \/a |xargs -n 1 cp -i index.html

If copying to a arbitrary/disjoint set of directories you'll need Robert Gamble's suggestion.

如果复制到任意/不相交的目录集,您将需要 Robert Gamble 的建议。

回答by Behrooz

For example if you are in the parent directory of you destination folders you can do:

例如,如果您位于目标文件夹的父目录中,则可以执行以下操作:

for i in $(ls); do cp sourcefile $i; done

对于我在 $(ls); 做 cp 源文件 $i; 完毕

回答by ddavison

No - you cannot.

你不能。

I've found on multiple occasions that I could use this functionality so I've made my own tool to do this for me.

我多次发现我可以使用此功能,因此我制作了自己的工具来为我执行此操作。

http://github.com/ddavison/branch

http://github.com/ddavison/branch

pretty simple -
branch myfile dir1 dir2 dir3

很简单——
branch myfile dir1 dir2 dir3

回答by Isaac

if you want to copy multiple folders to multiple folders one can do something like this:

如果要将多个文件夹复制到多个文件夹,可以执行以下操作:

echo dir1 dir2 dir3 | xargs -n 1 cp -r /path/toyourdir/{subdir1,subdir2,subdir3}

回声目录 1 目录 2 目录 3 | xargs -n 1 cp -r /path/toyourdir/{subdir1,subdir2,subdir3}