Tuesday, May 7, 2024
92
rated 0 times [  92] [ 0]  / answers: 1 / hits: 150876  / 3 Years ago, thu, july 8, 2021, 1:20:07

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?


More From » command-line

 Answers
1

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

[#26572] Friday, July 9, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ingwhin

Total Points: 332
Total Questions: 112
Total Answers: 115

Location: Burkina Faso
Member since Tue, Apr 26, 2022
2 Years ago
;