Copy Files To Multiple Folders At The Same Time in Linux

Save time and duplicate your files by copying them in different folders at the same time with commands in Linux.

One of the everyday tasks that we execute in Linux, regardless of the distro used, is to copy files either by backup themes or by having a new organization of the information. Although we can exchange information between different systems, the truth is that sometimes we waste even more time duplicating our information.

Copy File Command in Linux

When we copy a file, we know that the parameter we use is cp (Copy-Copy) and we indicate the destination path.

In this case, we have an image called Solvetic.png on the desktop, and we want to copy it into a folder called “Pruebas,” we will use the following syntax:

cp " Origin" "Destination"

We can see that merely we copy files in Linux, in this case in Ubuntu 16.

But, what happens if we want to copy that same file to more than one destination Ubuntu 16 or in any distro?

Although we can do it manually it becomes a tedious task, suppose we want to copy that file to the “Prueba” and “Pruebas” folders, we should manually enter each line:

This process, apart from being repetitive, will take more time than required.

In this tutorial, we will analyze how to copy a file to different directories in a single line in Linux.

Copy a file to Multiple Folders using echo command in Linux

To execute this task more efficiently we will use the echo command with some additional parameters.

The general syntax is:

echo path1 path2 etc | xargs -n 1 cp file_to_copy

The description of this line is as follows:

  • The echo command is responsible for writing on the Linux screen and in this case we put the vertical bar | to indicate that it takes the values of xargs.
  • The xargs parameter is responsible for copying using the cp command according to the number of destinations added.
  • The -n 1 parameter indicates that only one of these arguments is added to the cp command.

In our example, the syntax will be as follows:

echo /home/Solvetic/Solvetic /home/Solvetic/Pruebas /home/Solvetic/Escritorio/Test | xargs –n 1 cp /home/Solvetic/Escritorio/Solvetic.png

If we access any of the destination folders we will see the file that we have copied:

When using this syntax, if the file we are copying exists in one of the destination folders, it will be replaced automatically.

If we want the system to indicate whether or not we want to replace the file, we will use the -i parameter after cp. We can see that it is asking if we are sure to replace the destination file.

Another parameter that we can use is -n which prevents the file from being automatically overwritten if it already exists in the destination.

This is useful if the files are large because we avoid overloading the network and thus affect the performance of the system.

We can see with a single command how we can copy a file to different destinations automatically allowing thus having more free time for other administrative tasks.

In this way, we can have the same file in different directories or folders at the same time without having to open them.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *