How to copy a file to multiple directories using t

2019-01-09 22:10发布

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?

标签: linux bash shell
21条回答
甜甜的少女心
2楼-- · 2019-01-09 22:25

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:

find ./fs*/* -type d -print0 | xargs -0 -n 1 cp test 

Where test is the file to copy
And ./fs*/* the directories to copy to

The 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.

查看更多
该账号已被封号
3楼-- · 2019-01-09 22:25

If you want to do it without a forked command:

tee <inputfile file2 file3 file4 ... >/dev/null

查看更多
Viruses.
4楼-- · 2019-01-09 22:26
ls -d */ | xargs -iA cp file.txt A
查看更多
【Aperson】
5楼-- · 2019-01-09 22:27

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}

查看更多
小情绪 Triste *
6楼-- · 2019-01-09 22:28

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.

$ tar cf - src | tee >( cd dest1 ; tar xf - ) >( cd dest2 ; tar xf - ) | ( cd dest3 ; tar xf - )

(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).

查看更多
你好瞎i
7楼-- · 2019-01-09 22:28

This will copy to the immediate sub-directories, if you want to go deeper, adjust the -maxdepth parameter.

find . -mindepth 1 -maxdepth 1 -type d| xargs -n 1 cp -i index.html

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

find . -mindepth 1 -maxdepth 1 -type d| grep \/a |xargs -n 1 cp -i index.html

If copying to a arbitrary/disjoint set of directories you'll need Robert Gamble's suggestion.

查看更多
登录 后发表回答