How do I Copy a Particular File to Multiple Directories on Linux

Whenever I have to copy a particular file to multiple directories, I literally feel what a clunky job I have to do. Manually copying a single file to multiple locations is a total waste of time when you know you have to move this file across hundreds of directories.

Anyway, I need to do this, so there is no way to escape from this, so let’s be brave and find the solution to overcome this hurdle in the following article.

As you know, bare usage of cp or mv commands is not reliable for copying or moving content to multiple locations at once. However, with a little tweak, you can use the mv or cp command to move or copy files at multiple locations, which we will see in the subsequent section. Apart from that, you can also use findtee, and usage of echo and xargs commands to achieve your desired result.

Let’s recreate a problem  

For sake of your understanding, I have created multiple directories in my home directory, which I have kept the names dir-1, dir-2, dir-3, dir-4, and dir-5 and we will copy the file name called “TRENDOCEANS.txt” to all these created directories.

If you also want to replicate the same scenarios in your systems, then execute the following commands in the given order:

1. Create five different directories

$ mkdir dir-1 dir-2 dir-3 dir-4 dir-5

2. Create a sample file called TRENDOCEANS.txt

$ touch TRENDOCEANS.txt

Once you have followed the above steps, your screen will look like the below image after executing the below command.

$ ls -d dir-*
$ ls -l TRENDOCEANS.txt

The behaviour of the above command:

Created Problems
Sample files

The problem starts when I invoke the below command to copy TRENDOCEANS.txt to a newly created directory, but the command didn’t execute as per my intention. It just copied TRENDOCEANS.txt to the last directory, which is dir-5/.

$ cp TRENDOCEANS.txt dir-1/ dir-2/ dir-3/ dir-4/ dir-5/

Let’s find a solution

After getting aware of the scenario, let’s find a simple and practical way to solve this problem. Therefore, we have listed four different ways to solve this kind of problem using some of the basic commands:

Take the help of the tee command to copy files to multiple locations

First, start with the basic command tee, which is commonly used to read data from STDIN and save the output to files along with displaying the output to the screen (STDOUT).

The syntax is pretty simple. You just need to use the tee command and specify the path of the directory where you want to copy the respective file. After that, you need to use the redirection command, which will take the input from the filename and copy the inner content to a corresponding file.

$ tee /path/of/directory/filename /path/of/directory/filename < /path/of/filename

Not getting what I was trying to say? Then let me show you how to use the tee command to copy a particular file to multiple locations at once with the above-created sample file.

As you know, I have created multiple directories in my home directory; therefore, I have used ~ operators to assign a home address and declare the following path.

$ tee ~/dir-1/TRENDOCEANS.txt ~/dir-2/TRENDOCEANS.txt ~/dir-3/TRENDOCEANS.txt ~/dir-4/TRENDOCEANS.txt ~/dir-5/TRENDOCEANS.txt < ~/TRENDOCEANS.txt

The behaviour of the above command you can check by invoking the below command:

$ ls -l dir-*
Result after using tee command
Result after using tee command

After using the tee command, you may encounter with two kinds of problems. One of them is that you have to manually type the file names, and the second one is that file content gets printed on the screen, which I didn’t like at all, but it can be useful in some scenarios.

However, you can suppress the output by redirecting output to a special file /dev/null, which will prevent the output from getting printed on your screen.

$ tee ~/dir-1/TRENDOCEANS.txt ~/dir-2/TRENDOCEANS.txt ~/dir-3/TRENDOCEANS.txt ~/dir-4/TRENDOCEANS.txt ~/dir-5/TRENDOCEANS.txt < ~/TRENDOCEANS.txt > /dev/null

Take the help of the find and cp commands, copy files to multiple locations

A problem with the tee command is that you need to specify the filename along with the path where you want to copy a file, which can be a bit problematic if the file name gets changed while writing the path.

To avoid that, you can use the find and cp commands.

So let’s see how to make a perfect combination to copy a file to multiple locations at once using find and cp commands. For that reason, I have put the command syntax which we are going to use in this section.

$ find /path/where/you/want/to/copy -exec cp "/path/of/the/file/which/you/want/to/copy" {} \;

First of all, provide the location where you want to copy the file to find the command. After that, use the -exec option, which will execute the copy action to the specified location.

Now let me execute the following action on my system by following the above syntax:

$ find ~/dir-1 ~/dir-2/ ~/dir-3 ~/dir-4 ~/dir-5 -exec cp ~/TRENDOCEANS.txt {} \;

Once you execute the command, check the status by using the ls -l command.

Wow, it is better than the tee command, but there is a slight problem which you should be aware of before using the above command like it can copy files to multiple subdirectoreis .

This means if dir-1 has two subdirectories, then the file “TRENDOCEANS.txt” will get copied into the subdirectory, which may not be required, and to avoid that, you can use -maxdepth like the below command.

$ find ~/dir-1 ~/dir-2/ ~/dir-3 ~/dir-4 ~/dir-5 -maxdepth 0 -exec cp ~/TRENDOCEANS.txt {} \;

Take the help of the echo, xargs, and cp commands to copy files to multiple locations

Alternatively, you can also use echoxargs, and cp commands to copy files to multiple locations at once, which is also quite easy and simple.

You just need to use the echo command to feed in the location of the directory and, after that, using pipe operation, the data will be shared with the xargs command, which will copy the data one by one to the specified location.

For instance, let me copy “TRENDOCEANS.txt” to multiple directories, which are dir-1/ dir-2/ dir-3/ dir-4/ and dir-5/ by running the below code:

 $ echo dir-1 dir-2 dir-3/ dir-4 dir-5 | xargs -n 1 cp TRENDOCEANS.txt

Take the help of the loop and cp commands, you can copy files to multiple locations.

Lastly, you can also use the loop condition to copy a particular file at multiple locations without any hassle. You just need to know the command syntax, and your work is done.

I think the below command is self-explanatory if you are aware of shell scripting, but anyway, I’ll explain to you how the below command will take action when you execute it on your terminal screen.

In the below instance, the loop will run five times because “i” has enlisted five directories, on which the cp command will execute the specified instruction.

$ for i in ~/dir-1 ~/dir-2 ~/dir-3 ~/dir-4 ~/dir-5; do cp ~/TRENDOCEANS.txt $i; done 

Quite easy.

Wrap up

That’s all for now!

In this article, you have learned how to copy a specific file to multiple directories at once in Linux. If you are facing any issues while following this article do let me know in the comment section.

If you found this useful, please share this article😇

Leave a Reply