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

s2 0

Copying a file in a Linux operating system is easy, whether from the command line or graphically. However, some users prefer the command line to perform almost all tasks. The command line is not only an easy, but also a faster way to perform a task. Cp is the command in Linux 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 achieve this on Linux.

We ran the commands and procedures mentioned in this article on a Debian 10 operating system, but they will work on any other Linux distribution as well.

First, let’s see how we can use the cp command to copy the files from one location to another. The general syntax for copying the files with the cp command is:

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

An example of this would be copying testfile1 from the Documents directory to the Downloads directory. To do this, I ran the following command in the terminal:

$ cp ~/Documents/testfile1 ~/Downloads/

Now if we need to copy the file to multiple locations, we need to run the commands multiple times. For example, in the following example we are copying Test File1 from Documents to two different locations, i.e. Downloads and Desktop. To do this, we need to run the cp commands twice in the terminal:

$ cp ~/Documents/testfile1 ~/Downloads/

$ cp ~/Documents/testfile1 ~/Desktop/

Copying a file to multiple locations with the echo command

Copying a file to two locations with the cp command is still acceptable, but let’s say we need to copy the file to four, five, or more locations. In this case we have another solution that uses 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 to display it on the screen. But here in this example we’re going to use it to output to the xargs command via the | Symbol. The xargs are entered three times by 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 tells the cp command to take one argument at a time.

Note that this command will overwrite an existing file with the same name in the destination directory. So it is better to have a backup of the important file already.

In the following example we use this command to copy the test file1 from the documents directory to three different directories, namely desktop, downloads and the music directory. To do this, we ran the following command:

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

Copy the file to multiple locations

That’s all! Now we’ve learned how to copy a file to multiple directories with one command. So now you don’t have to write multiple commands to perform the simplest task of copying a file to multiple locations.advertising

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