Copying one File Simultaneously to Multiple Locations through Ubuntu Command Line

Copy File Simultaniously on Linux

As a command line newbie, you might feel that the same task you quickly used to perform through the graphical interface might ask for a lot of commands in the command line. However, as you slowly become a command line power user through learning, practice, and experience, you will start to notice that the same tasks can be performed very quickly through some very simple yet useful shortcuts. In this article, we will describe one such case that apparently might need a lot of commands to run but in actual, one simple command can achieve the task for you.

At times, we require copying a single file to multiple locations on our system. So does that mean, we need to use the cp command multiple times? The answer is no! Let us read further to find a solution.

The commands mentioned in this article have been run of an Ubuntu 18.04 LTS system.

How to copy one file simultaneously to multiple locations

We all know how the cp command lets us copy a file to a new location through the following syntax:

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

Here I am copying a sample text file from my Downloads folder to the Documents folder:

Copy file to one location

Now if I want to copy the same file to two different locations instead of one, the probable solution seems using the cp command twice.

Here I am using the cp command twice to copy a sample text file from the Downloads folder to the Public and Desktop folders:

copy file twice

Copying the same file to two locations by using the cp command twice still seems logical but let us suppose we have to copy the file to three, five, or even more locations. Here is how a single command can achieve this purpose.


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

In the following example, I will use this command to copy a sample text file from my Downloads folder to three different folders simultaneously:

copy file to two locations with one command

We have used the echo command and the xargs command in one line to attain our purpose.

How the command works?

The echo command prints the output to the screen but in our example, we are using it to feed output to the xargs command through the | symbol. The xargs command will take input three times from the echo command and perform the cp operation thrice, copying the sample text to three different locations. The n count tells the cp command to take one argument at a time.

Please note that this command will overwrite an already existing file by the same name in the destination folder. Therefore, it is good practice to always take backup of your important files. The i option that we used for asking before the overwrite operation does not work with the xargs command.

However, there is one use of the command that can help you avoid overwriting a file if it already exists in the destination folder; the n option before the source file.


$ echo [destination1] [desctination2] [destiantion3]..... | xargs -n 1 cp n [/lcoation/sourcefile]


using xargs command

The n option is very useful while you are copying very large files from one location to another, especially over a network. This way you can avoid the resources wasted on copying and then replacing an already existing file.

After running this tutorial, you have become one step closer to becoming a command line guru. Now you do not need to write multiple commands to perform the simple task of copying one file to different locations. You can merge the echo and xargs command, as we described, in order to have a one-command solution to your problem.