Ubuntu – How to copy a file to multiple folders using the command line

command linecp

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?

Best Answer

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
Related Question