是否可以使用cp命令将一个文件复制到多个目录?
我尝试了以下方法,但没有奏效:
cp file1 /foo/ /bar/
cp file1 {/foo/,/bar}
我知道可以使用for循环,或者find。但是可以使用gnu cp命令吗?
是否可以使用cp命令将一个文件复制到多个目录?
我尝试了以下方法,但没有奏效:
cp file1 /foo/ /bar/
cp file1 {/foo/,/bar}
我知道可以使用for循环,或者find。但是可以使用gnu cp命令吗?
当前回答
通配符也适用于Roberts代码
echo ./fs*/* | xargs -n 1 cp test
其他回答
我会根据我在https://superuser.com/questions/32630/parallel-file-copy-from-single-source-to-multiple-targets上看到的答案使用cat和tee,而不是cp。
例如:
cat inputfile | tee outfile1 outfile2 > /dev/null
ls -d */ | xargs -iA cp file.txt A
使用bash脚本
DESTINATIONPATH[0]="xxx/yyy"
DESTINATIONPATH[1]="aaa/bbb"
..
DESTINATIONPATH[5]="MainLine/USER"
NumberOfDestinations=6
for (( i=0; i<NumberOfDestinations; i++))
do
cp SourcePath/fileName.ext ${DESTINATIONPATH[$i]}
done
exit
另一种方法是使用cat和tee,如下所示:
cat <source file> | tee <destination file 1> | tee <destination file 2> [...] > <last destination file>
我认为这将是相当低效的,尽管,因为工作将被分割在几个进程(一个目的地)和硬盘驱动器将写入几个文件一次在不同的部分盘片。然而,如果你想把一个文件写到几个不同的驱动器上,这个方法可能会非常有效(因为所有的拷贝都可以并发进行)。
这将复制到直接子目录,如果你想深入,调整-maxdepth参数。
find . -mindepth 1 -maxdepth 1 -type d| xargs -n 1 cp -i index.html
如果您不想复制到所有目录,希望您可以过滤您不感兴趣的目录。复制到所有以a开头的文件夹
find . -mindepth 1 -maxdepth 1 -type d| grep \/a |xargs -n 1 cp -i index.html
如果复制到任意/不连贯的目录集,您将需要Robert Gamble的建议。