Linux commands to copy one file to many files

2020-05-13 13:24发布

Is there a one-line command/script to copy one file to many files on 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?

标签: linux cp
9条回答
成全新的幸福
2楼-- · 2020-05-13 14:20

You can improve/simplify the for approach (answered by @ruakh) of copying by using ranges from bash brace expansion:

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

This copies file into file1, file2, ..., file10.

Resource to check:

查看更多
再贱就再见
3楼-- · 2020-05-13 14:20

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

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

eval 'cp file1 '{file2,file3}';'
查看更多
The star\"
4楼-- · 2020-05-13 14:21

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
查看更多
登录 后发表回答