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?
To use copying with
xargs
to directories using wildcards on Mac OS, the only solution that worked for me with spaces in the directory name is:Where
test
is the file to copyAnd
./fs*/*
the directories to copy toThe problem is that xargs sees spaces as a new argument, the solutions to change the delimiter character using
-d
or-E
is unfortunately not properly working on Mac OS.If you want to do it without a forked command:
tee <inputfile file2 file3 file4 ... >/dev/null
if you want to copy multiple folders to multiple folders one can do something like this:
echo dir1 dir2 dir3 | xargs -n 1 cp -r /path/toyourdir/{subdir1,subdir2,subdir3}
Not using cp per se, but...
This came up for me in the context of copying lots of Gopro footage off of a (slow) SD card to three (slow) USB drives. I wanted to read the data only once, because it took forever. And I wanted it recursive.
(And you can add more of those >() sections if you want more outputs.)
I haven't benchmarked that, but it's definitely a lot faster than cp-in-a-loop (or a bunch of parallel cp invocations).
This will copy to the immediate sub-directories, if you want to go deeper, adjust the
-maxdepth
parameter.If you don't want to copy to all directories, hopefully you can filter the directories you are not interested in. Example copying to all folders starting with
a
If copying to a arbitrary/disjoint set of directories you'll need Robert Gamble's suggestion.