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

52

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

cp file1 file2 file3

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


一个问题是,您是否希望file2和file3占据独立的块? - Ahmed Masud
13个回答

82

这个问

cp file1 file2 ; cp file1 file3

将计数作为“一行命令/脚本”吗?那么

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

或者,如果稍微宽松一点的定义是“复制”:

tee <file1 file2 file3 >/dev/null

2
@J.F.Sebastian:是的,但既然OP显然希望它能用于不止两个目标文件,我选择了突出支持多个文件的形式。也许我应该写成tee <file1 >file2 file3 file4并且放弃两个文件的并行? - ruakh
1
不确定这是否仍然相关,但我想指出使用tee解决方案将无法保留初始文件的权限。 - bioShark
@bioShark:对,它只复制文件的内容。这就是为什么我将其描述为“稍微宽松的‘复制’”的原因。 - ruakh
2
只是为了好玩,如果你需要一个大文件列表,可以使用tee <sourcefile.jpg targetfiles{01-50}.jpg >/dev/null - styks

14

如果您需要一个大文件列表,可以玩玩:

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

但有一个小错误。应该是:

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

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


9

您可以使用来自 Bash 花括号扩展的范围来改进/简化通过 for 方法(由@ruakh回答)进行复制:

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

这将把file复制到file1,file2,...,file10中。
查看资源:

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

3

(不使用循环)

在Linux中将一个文件(fileA.txt)的内容复制到多个文件(fileB.txt, fileC.txt, fileD.txt),请使用以下组合的cattee命令:

cat fileA.txt | tee fileB.txt fileC.txt fileD.txt >/dev/null
  • 适用于任何文件扩展名
  • 只有文件名和扩展名会改变,其他所有内容都保持不变。

2
您可以使用shift
file=$1
shift
for dest in "$@" ; do
    cp -r $file $dest
done

2
shift 的作用是什么? - Peter Mortensen

2

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

的意思是将文件file1的内容输出,并分别复制到file2、file3、file4和file5中,同时将所有输出重定向到/dev/null中。

它可能能够工作,但它非常缺乏灵活性。如果你要复制大文件,它很可能效率低下。 - Stephen C
@StephenC,它更接近原始查询。 - Isaiah Taylor
如果您使用cat和tee,那么可以这样做:cat file1 | tee file2 file3 file4 >/dev/null - Ng Sek Long

1

使用以下类似的方法。它适用于zsh。

cat file > firstCopy > secondCopy > thirdCopy

或者

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

对于小文件很有效。

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


1
它假设shell是zsh - Stephane Chazelas
文件大小为什么很重要?受RAM大小限制吗? - Peter Mortensen
由于猫的处理 - Thomas

1
我建议创建一个通用脚本和一个基于该脚本的函数(empty-files),以清空任意数量的目标文件。
将脚本命名为copy-from-one-to-many并将其放在您的PATH中。
#!/bin/bash -e
#  _ _____     
# | |___ /_  __  
# | | |_ \ \/ /  Lex Sheehan (l3x)
# | |___) >  <   https://github.com/l3x
# |_|____/_/\_\  
#
# Copy the contents of one file to many other files.

source=$1
shift
for dest in "$@"; do
    cp $source $dest
done

exit

注意事项

shift 命令可以将参数列表("$@")的第一个元素(源文件路径)移除。

清空多个文件的示例:

创建带有内容的 file1、file2、file3、file4 和 file5 文件:

for f in file{1..5}; do echo $f > "$f"; done

清空多个文件:

copy-from-one-to-many /dev/null file1 file2 file3 file4 file5

更轻松地清空多个文件:

# Create files with content again
for f in file{1..5}; do echo $f > "$f"; done 

copy-from-one-to-many /dev/null file{1..5}

基于从一个文件复制到多个文件的方法,创建一个空文件函数
function empty-files()
{
    copy-from-one-to-many /dev/null "$@"
}

使用示例

# Create files with content again
for f in file{1..5}; do echo $f > "$f"; done 
# Show contents of one of the files
echo -e "file3:\n $(cat file3)"

empty_files file{1..5}
# Show that the selected file no longer has contents
echo -e "file3:\n $(cat file3)"

不要只是抄袭代码,改进它;用示例文档化并分享它。- l3x

这里有一个版本,每个cp命令都会在前面加上sudo

#!/bin/bash -e
# Filename: copy-from-one-to-may
#  _ _____     
# | |___ /_  __  
# | | |_ \ \/ /  Lex Sheehan (l3x)
# | |___) >  <   https://github.com/l3x
# |_|____/_/\_\  
#
# Copy the contents of one file to many other files.
# Pass --sudo if you want each cp to be perfomed with sudo
# Ex: copy-from-one-to-many $(mktemp) /tmp/a /tmp/b /tmp/c --sudo

if [[ "$*" == *--sudo* ]]; then
    maybe_use_sudo=sudo
fi

source=$1
shift
for dest in "$@"; do
    if [ $dest != '--sudo' ]; then
      $maybe_use_sudo cp $source $dest
    fi
done

exit

0
你可以使用标准的脚本命令来代替这个功能:
Bash:
 for i in file2 file3 ; do cp file1 $i ; done

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接