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?
将前两个文件复制到第三个文件中。有没有办法将第一个文件复制到其余文件中?
9 个解决方案
#1
50
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
3
for FILE in "file2" "file3"; do cp file1 $FILE; done
#3
1
You can use shift
:
你可以使用shift:
file=$1
shift
for dest in "$@" ; do
cp -r $file $dest
done
#4
1
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:52tee
/ dev / null- Kelvin 2月12日19:52
But there's a little typo. Should be:
但是有一点错字。应该:
tee <sourcefile.jpg targetfiles{01..50}.jpg >/dev/null
tee
/ dev / null
And as mentioned above, that doesn't copy permissions.
如上所述,这不会复制权限。
#5
0
You can use standard scripting commands for that instead:
您可以使用标准脚本命令:
Bash:
击:
for i in file2 file3 ; do cp file1 $i ; done
#6
0
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}';'
#7
0
Use something like the following. It works on zsh.
使用类似下面的内容。它适用于zsh。
cat file > firstCopy > secondCopy > thirdCopy
cat file> firstCopy> secondCopy> thirdCopy
or
要么
cat file > {1..100} - for filenames with numbers.
cat file> {1..100} - 用于带数字的文件名。
It's good for small files.
这对小文件很有用。
You should use the cp script mentioned earlier for larger files.
您应该使用前面提到的cp脚本来处理更大的文件。
#8
0
You can improve/simplify the for
approach (answered by @ruakh) of copying by using ranges from bash brace expansion:
您可以使用bash大括号扩展中的范围来改进/简化复制方法(由@ruakh回答):
for f in file{1..10}; do cp file $f; done
This copies file
into file1, file2, ..., file10
.
这会将文件复制到file1,file2,...,file10中。
Resource to check:
要检查的资源:
- http://wiki.bash-hackers.org/syntax/expansion/brace#ranges
- http://wiki.bash-hackers.org/syntax/expansion/brace#ranges
#9
0
cat file1 | tee file2 | tee file3 | tee file4 | tee file5 >/dev/null
cat file1 | tee file2 | tee file3 | tee file4 | tee file5> / dev / null
#1
50
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
3
for FILE in "file2" "file3"; do cp file1 $FILE; done
#3
1
You can use shift
:
你可以使用shift:
file=$1
shift
for dest in "$@" ; do
cp -r $file $dest
done
#4
1
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:52tee
/ dev / null- Kelvin 2月12日19:52
But there's a little typo. Should be:
但是有一点错字。应该:
tee <sourcefile.jpg targetfiles{01..50}.jpg >/dev/null
tee
/ dev / null
And as mentioned above, that doesn't copy permissions.
如上所述,这不会复制权限。
#5
0
You can use standard scripting commands for that instead:
您可以使用标准脚本命令:
Bash:
击:
for i in file2 file3 ; do cp file1 $i ; done
#6
0
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}';'
#7
0
Use something like the following. It works on zsh.
使用类似下面的内容。它适用于zsh。
cat file > firstCopy > secondCopy > thirdCopy
cat file> firstCopy> secondCopy> thirdCopy
or
要么
cat file > {1..100} - for filenames with numbers.
cat file> {1..100} - 用于带数字的文件名。
It's good for small files.
这对小文件很有用。
You should use the cp script mentioned earlier for larger files.
您应该使用前面提到的cp脚本来处理更大的文件。
#8
0
You can improve/simplify the for
approach (answered by @ruakh) of copying by using ranges from bash brace expansion:
您可以使用bash大括号扩展中的范围来改进/简化复制方法(由@ruakh回答):
for f in file{1..10}; do cp file $f; done
This copies file
into file1, file2, ..., file10
.
这会将文件复制到file1,file2,...,file10中。
Resource to check:
要检查的资源:
- http://wiki.bash-hackers.org/syntax/expansion/brace#ranges
- http://wiki.bash-hackers.org/syntax/expansion/brace#ranges
#9
0
cat file1 | tee file2 | tee file3 | tee file4 | tee file5 >/dev/null
cat file1 | tee file2 | tee file3 | tee file4 | tee file5> / dev / null