Linux命令将一个文件复制到多个文件

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

Linux commands to copy one file to many files

linuxcp

提问by user121196

Is there a one-line command/script to copy one file to many files on Linux?

在 Linux 上是否有一行命令/脚本可以将一个文件复制到多个文件?

cp file1 file2 file3

copies the first two files into the third. Is there a way to copy the first file into the rest?

将前两个文件复制到第三个文件中。有没有办法将第一个文件复制到其余文件中?

采纳答案by ruakh

Does

cp file1 file2 ; cp file1 file3

count as a "one-line command/script"? How about

算作“单行命令/脚本”?怎么样

for file in file2 file3 ; do cp file1 "$file" ; done

?

?

Or, for a slightly looser sense of "copy":

或者,对于“复制”的稍微宽松的感觉:

tee <file1 file2 file3 >/dev/null

回答by knittl

The simplest/quickest solution I can think of is a for loop:

我能想到的最简单/最快的解决方案是 for 循环:

for target in file2 file3 do; cp file1 "$target"; done

A dirty hack would be the following (I strongly advise against it, and only works in bash anyway):

一个肮脏的黑客将如下(我强烈建议不要这样做,无论如何只能在 bash 中工作):

eval 'cp file1 '{file2,file3}';'

回答by Wes Hardaker

You can use standard scripting commands for that instead:

您可以改为使用标准脚本命令:

Bash:

重击:

 for i in file2 file3 ; do cp file1 $i ; done

回答by Matt

for FILE in "file2" "file3"; do cp file1 $FILE; done

回答by Matt

You can use shift:

您可以使用shift

file=
shift
for dest in "$@" ; do
    cp -r $file $dest
done

回答by Thomas

Use something like the following. It works on zsh.

使用类似以下内容。它适用于 zsh。

cat file > firstCopy > secondCopy > thirdCopy

cat 文件 > firstCopy > secondCopy >thirdCopy

or

或者

cat file > {1..100} - for filenames with numbers.

cat 文件 > {1..100} - 用于带数字的文件名。

It's good for small files.

它适用于小文件。

You should use the cp script mentioned earlier for larger files.

对于较大的文件,您应该使用前面提到的 cp 脚本。

回答by Yevgen

just for fun, if you need a big list of files:

tee <sourcefile.jpg targetfiles{01-50}.jpg >/dev/null- Kelvin Feb 12 at 19:52

只是为了好玩,如果您需要大量文件:

tee <sourcefile.jpg targetfiles{01-50}.jpg >/dev/null-开尔文 2 月 12 日 19:52

But there's a little typo. Should be:

但是有一点错别字。应该:

tee <sourcefile.jpg targetfiles{01..50}.jpg >/dev/null

tee <sourcefile.jpg targetfiles{01..50}.jpg >/dev/null

And as mentioned above, that doesn't copy permissions.

如上所述,这不会复制权限。

回答by Lirt

You can improve/simplify the forapproach (answered by @ruakh) of copying by using rangesfrom bash brace expansion:

可以提高/简化for通过使用复制的方法(由@ruakh回答)范围bash中括号扩展

for f in file{1..10}; do cp file $f; done

This copies fileinto file1, file2, ..., file10.

这复制filefile1, file2, ..., file10.

Resource to check:

要检查的资源:

回答by Isaiah Taylor

cat file1 | tee file2 | tee file3 | tee file4 | tee file5 >/dev/null

cat file1 | tee file2 | tee file3 | tee file4 | tee file5 >/dev/null