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
If you want to do it without a forked command:
tee <inputfile file2 file3 file4 ... >/dev/null
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
As far as I can see it you can use the following:
ls | xargs -n 1 cp -i file.dat
The -i
option of cp
command means that you will be asked whether to overwrite a file in the current directory with the file.dat
. Though it is not a completely automatic solution it worked out for me.
If all your target directories match a path expression — like they're all subdirectories of path/to
— then just use find
in combination with cp
like this:
find ./path/to/* -type d -exec cp [file name] {} \;
That's it.
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.
These answers all seem more complicated than the obvious:
for i in /foo /bar; do cp "$file1" "$i"; done