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?
These answers all seem more complicated than the obvious:
No,
cp
can copy multiple sources but will only copy to a single destination. You need to arrange to invokecp
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 combinecp
withxargs
:Will copy
file1
todir1
,dir2
, anddir3
.xargs
will callcp
3 times to do this, see the man page forxargs
for details.If you need to be specific on into which folders to copy the file you can combine find with one or more greps. For example to replace any occurences of favicon.ico in any subfolder you can use:
I like to copy a file into multiple directories as such:
cp file1 /foo/; cp file1 /bar/; cp file1 /foo2/; cp file1 /bar2/
And copying a directory into other directories:cp -r dir1/ /foo/; cp -r dir1/ /bar/; cp -r dir1/ /foo2/; cp -r dir1/ /bar2/
I know it's like issuing several commands, but it works well for me when I want to type 1 line and walk away for a while.
I would use
cat
andtee
based on the answers I saw at https://superuser.com/questions/32630/parallel-file-copy-from-single-source-to-multiple-targets instead ofcp
.For example: