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条回答
Deceive 欺骗
2楼-- · 2020-05-13 13:58

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.

查看更多
不美不萌又怎样
3楼-- · 2020-05-13 14:00

You can use shift:

file=$1
shift
for dest in "$@" ; do
    cp -r $file $dest
done
查看更多
走好不送
4楼-- · 2020-05-13 14:07
for FILE in "file2" "file3"; do cp file1 $FILE; done
查看更多
闹够了就滚
5楼-- · 2020-05-13 14:10

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

查看更多
The star\"
6楼-- · 2020-05-13 14:10

You can use standard scripting commands for that instead:

Bash:

 for i in file2 file3 ; do cp file1 $i ; done
查看更多
你好瞎i
7楼-- · 2020-05-13 14:12

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.

查看更多
登录 后发表回答