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?
As far as I can see it you can use the following:
The
-i
option ofcp
command means that you will be asked whether to overwrite a file in the current directory with thefile.dat
. Though it is not a completely automatic solution it worked out for me.ls -db di*/subdir | xargs -n 1 cp File
-b
in case there is a space in directory name otherwise it will be broken as a different item by xargs, had this problem with the echo versionUsing a bash script
Essentially equivalent to the xargs answer, but in case you want parallel execution:
So, for example, to copy file1 into all subdirectories of current folder (including recursion):
N.B.: This probably only conveys any noticeable speed gains for very specific use cases, e.g. if each target directory is a distinct disk.
It is also functionally similar to the '-P' argument for xargs.
Another way is to use cat and tee as follows:
I think this would be pretty inefficient though, since the job would be split among several processes (one per destination) and the hard drive would be writing several files at once over different parts of the platter. However if you wanted to write a file out to several different drives, this method would probably be pretty efficient (as all copies could happen concurrently).
No - you cannot.
I've found on multiple occasions that I could use this functionality so I've made my own tool to do this for me.
http://github.com/ddavison/branch
pretty simple -
branch myfile dir1 dir2 dir3