Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a file to multiple directories using the gnu cp command

Tags:

linux

bash

shell

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?

like image 416
Tom Feiner Avatar asked Oct 12 '08 16:10

Tom Feiner


People also ask

How do I copy a file to multiple directories in Linux?

The another way to copy files to multiple locations is by using echo , xargs and cp commands. As you already know, the cp command is used to copy the files and directories, and xargs command is used to build and execute command lines from standard input.

How do I copy a file to multiple folders at once?

If you need to copy a file to multiple folders, you can hold down the Ctrl key, and drag the file or folder on to each folder you want to copy it to.

Can cp command copy directories?

With cp command, you can copy a directory and an entire subdirectory with its content and everything beneath it. cp and rsync are one of the most popular commands for copying files and directory.


10 Answers

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.

like image 126
Robert Gamble Avatar answered Sep 25 '22 16:09

Robert Gamble


No, cp can copy multiple sources but will only copy to a single destination. You need to arrange to invoke cp multiple times - once per destination - for what you want to do; using, as you say, a loop or some other tool.

like image 22
moonshadow Avatar answered Sep 24 '22 16:09

moonshadow


Wildcards also work with Roberts code

echo ./fs*/* | xargs -n 1 cp test 
like image 37
Paul Avatar answered Sep 22 '22 16:09

Paul


I would use cat and tee based on the answers I saw at https://superuser.com/questions/32630/parallel-file-copy-from-single-source-to-multiple-targets instead of cp.

For example:

cat inputfile | tee outfile1 outfile2 > /dev/null
like image 26
deterb Avatar answered Sep 25 '22 16:09

deterb


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.

like image 29
Evgeny Avatar answered Sep 25 '22 16:09

Evgeny


These answers all seem more complicated than the obvious:

for i in /foo /bar; do cp "$file1" "$i"; done
like image 24
Waxrat Avatar answered Sep 25 '22 16:09

Waxrat


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 version

like image 37
thAAAnos Avatar answered Sep 24 '22 16:09

thAAAnos


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

like image 26
oggust Avatar answered Sep 25 '22 16:09

oggust


If you want to do it without a forked command:

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

like image 42
Taywee Avatar answered Sep 21 '22 16:09

Taywee


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.

like image 38
MegaCookie Avatar answered Sep 25 '22 16:09

MegaCookie