Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have tried to copy a file test.txt to multiple directories with one command:

cp ~/test.txt ~/folder1 ~/folder2

But I didn't succeed. Is there a way to do that in one command so I can copy a file or even a folder to multiple directories?

share|improve this question
Not easily. You may want to look into "rsync" for efficiently updating multiple existing copies of a folder –  Thorbjørn Ravn Andersen Mar 13 '14 at 10:15
Try learning a bit of bash-script. It can get very useful: for dest in folder1 folder2; do cp ~/test.txt ~/"$dest"; done –  Shahbaz Mar 13 '14 at 11:08

6 Answers 6

up vote 50 down vote accepted

cp can copy from multiple sources, but can't copy to multiple destinations. See man cp for more info.

The only bash command that I know which can copy/save to multiple destinations is tee.

You can use it in your case as follows:

tee ~/folder1/test.txt ~/folder2/test.txt < ~/test.txt

Note that tee also writes the input to the standard output (stdout). So if you don't want this, you can prevent it by redirecting standard output to /dev/null as follow:

tee ~/folder1/test.txt ~/folder2/test.txt < ~/test.txt >/dev/null
share|improve this answer
Clever, that's cheating! :) –  terdon Mar 11 '14 at 17:13
@terdon Only if you consider that I/O redirection is an independent command :) –  Radu Rădeanu Mar 11 '14 at 17:18
+1, clever! And if you use the >/dev/null works with binary files too, without messing up the terminal... –  Rmano Mar 11 '14 at 17:30
@nux Don't forget to use -r option with cp in case of directories –  Radu Rădeanu Mar 11 '14 at 19:08
@nux: tee unlike cp copies only file's content ignoring its mode, ownership, timestamps. To avoid repeating the filename: f=text.txt; <~/$f tee ~/folder1/$f > ~/folder2/$f –  J.F. Sebastian Mar 13 '14 at 9:26

Another way to achieve a copy to multiple locations is the following command :

find dir1 dir2 -exec cp file.txt {} \;

If dir1 or dir2 have sub-directories that you don't want the file copied into, add -maxdepth 0option :

find dir1 dir2 -maxdepth 0 -exec cp file.txt {} \;
share|improve this answer
Nice solution! Seems to me more perfect than with tee. –  Danatela Mar 11 '14 at 17:55
Indeed, no redirection is involved and it's still one command. –  Sylvain Pineau Mar 11 '14 at 18:03
@SylvainPineau In fact there are 3: one is find and for two times (in this case) cp. Not to say that find need a lot of time. –  Radu Rădeanu Mar 11 '14 at 18:13
If the definition of a "command" is an "exec" call, maybe, but this is "one command" in my book. –  kojiro Mar 11 '14 at 22:27
technically two commands (find and cp) but no pipes so "looks" like one command ;-) –  Michael Martinez Mar 12 '14 at 19:28

The command

cp ~/test.txt ~/folder1 ~/folder2

tries to copy two files (~/test.txt and ~/folder1) to the destination folder2. (And if ~/folder2 exists and is a directory you will have an "omitting directory" warning).

If you want to make multiple copies of the file test.txt, you have to use a loop or multiple commands...

for i in ~/folder1 ~/folder2; do cp  ~/test.txt $i; done 

(...and be careful if you have spaces embedded in the file names, you'll need quoting).

To copy whole directories you have to use the -r option:

for i in ~/folder1 ~/folder2; do cp -r ~/folder3 $i; done

this will create ~/folder1/folder3 and ~/folder2/folder3 with all the files included.

share|improve this answer
For the passing reader: According to the spec, a for loop is a compound command. Therefore this is still one command, and meets all the requirements for the question. –  kojiro Mar 11 '14 at 22:35
@nux, I do not agree on the corrections. 1) the two capitals are wrong, my intent was to interleave the commands into the phrase. 2) the ellipsis is a matter of style; please let my style in. –  Rmano Mar 13 '14 at 16:06
ok , am sorry man , i thought it will look better . –  nux Mar 13 '14 at 16:09

You can create a help script , or you can do it with xargs and a print function (in this case, echo ):

echo firstDir secondDir | xargs -n 1 cp test

This will make each directory as an argument to the cp function , using test file as a parameter.

share|improve this answer

After a long search this work like a Charm also !

for dir in *; do [ -d "$dir" ] && cp /path/file.txt "$dir" ; done

This will copy file.txt to every directory in your current location in terminal.

for dir in *; do [ -d "$dir" ] && cp -rf /path/folder "$dir" ; done

This will copy a folder to every sub directory in your current location in terminal.

I share it hope it helps others too .

share|improve this answer
In zsh, you can use for i in *(/); ... to loop over all the subdirectories, so you can avoid the [ -d ... test. Extended globbing is one of the reason I like it over bash. –  Rmano Mar 12 '14 at 2:43
can your right the code again in a command –  nux Mar 12 '14 at 12:13
In zsh, the first command of this answer can be simplified as for dir in *(/); do cp /path/file.txt "$dir"; done. See zsh.sourceforge.net/Intro/intro_2.html –  Rmano Mar 12 '14 at 14:01
You can eliminate the test in any Bourne derived shell with for dir in */; –  Henk Langeveld Mar 13 '14 at 8:05

Any problem with this:

cp source dest1
cp source dest2
share|improve this answer
In the general case, there are more than two directories. You wouldn't want to pass the afternoon copying around the same file manually, would you? :) –  edwin Jul 16 '14 at 16:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.