How to copy a file to multiple directories using the gnu cp command

Is it possible to copy a single file to multiple directories using the cp command ?

I tried the following , which did not work:

cp file1 /foo/ /bar/
cp file1 {/foo/,/bar}

I know it's possible using a for loop, or find. But is it possible using the gnu cp command?


No, cp can copy multiple sources but will only copy to a single destination. You need to arrange to invoke cp multiple times - once per destination - for what you want to do; using, as you say, a loop or some other tool.


You can't do this with cp alone but you can combine cp with xargs :

echo dir1 dir2 dir3 | xargs -n 1 cp file1

Will copy file1 to dir1 , dir2 , and dir3 . xargs will call cp 3 times to do this, see the man page for xargs for details.


通配符也适用于Roberts代码

echo ./fs*/* | xargs -n 1 cp test 
链接地址: http://www.djcxy.com/p/78328.html

上一篇: 使用find和sed递归重命名文件

下一篇: 如何使用gnu cp命令将文件复制到多个目录