How to copy a file with one command to multiple directories on Linux

Copying a file in a Linux OS is straightforward whether using the command line or the graphical way. However, some users prefer command line for performing nearly all tasks. Command-line not only offers the easy but quicker way of performing a task. Cp is the command in Linux that is used to copy a file from one location to another. But what if we need to copy a file to multiple locations. There is a way I can show how to achieve that in Linux.

We have run the commands and procedure mentioned in this article on a Debian 10 OS, but they will work on any other Linux distribution as well.

Let us first see how we can use the cp command to copy the files from one location to another. The general syntax for copying the files using cp command is :

$ cp ~[/location/sourcefile] ~[/destinationfolder]

An example of this would be to copy the testfile1 from the Documents directory to the Downloads directory. For that, I have run the following command in terminal:

$ cp ~/Documents/testfile1 ~/Downloads/

Copy one file

Now when we have to copy the file to multiple locations, we have to run the commands multiple times. For instance, in the following example, we are copying the testfile1 from Documents to two different locations i.e Downloads and Desktop. For that, we have to run the cp commands twice in Terminal:

$ cp ~/Documents/testfile1 ~/Downloads/

$ cp ~/Documents/testfile1 ~/Desktop/

Copy two files

Copying a file to multiple locations using the echo command

Copying a file to two locations by using the cp command is still acceptable but let’s suppose we have to copy the file to four, five or more locations. in that case, we have another solution that is using the echo command. The syntax of the command would be:

$ echo [destination1] [destination2] [destiantion3]..... | xargs -n 1 cp [/location/sourcefile]

The echo command is usually used in shell scripts to print a message or output to the screen. But here in this example, we will use it to feed output to the xargs command through the | symbol. The xargs will get input three times from the echo command and perform the cp operations three times, copying the test file to 3 different locations. the n flag in the above command will tell the cp command to take one argument at a time.

Make note that this command will overwrite an already existing file by the same name in the destination directory. Therefore, it is better to already take a backup of the important file.

In the following example, we are using this command to copy the testfile1 from the Documents directory to three different directories that are Desktop, Downloads and the Music directory. For that, we have run the following command:

$ $ echo [~/Desktop] [~/Downloads] [~/Music] | xargs -n 1 cp [/location/sourcefile]

Copy file to multiple locations

That is all there is to it! Now we have learned how to copy a file with one command to multiple directories. So now you do not have to write multiple commands for performing the simplest task of copying one file to multiple locations.