Linux commands to copy one file to many files

*爱你&永不变心* 提交于 2019-12-03 02:40:49

问题


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?


回答1:


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



回答2:


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

But there's a little typo. Should be:

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

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




回答3:


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



回答4:


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:

  • http://wiki.bash-hackers.org/syntax/expansion/brace#ranges



回答5:


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




回答6:


You can use shift:

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



回答7:


Use something like the following. It works on zsh.

cat file > firstCopy > secondCopy > thirdCopy

or

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

It's good for small files.

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




回答8:


You can use standard scripting commands for that instead:

Bash:

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



回答9:


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}';'


来源:https://stackoverflow.com/questions/9550540/linux-commands-to-copy-one-file-to-many-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!