Home Command line utilities How To Copy A File To Multiple Directories In Command Line On Linux
Copy A File To Multiple Directories In Command Line On Linux

How To Copy A File To Multiple Directories In Command Line On Linux

By sk

In this brief guide, I will be explaining how to copy a file to multiple directories in command line on Linux. The other day I wanted to copy a movie to two different folders in my Arch Linux desktop. As you already know, We can easily do it by right-clicking on the movie file, select Copy option and paste it on the destination directory/folder. However, I was curious to know that is there any other option to copy the file into multiple directories in one go. I thought It would help when I want to copy a single file into number of different directories at once. I dug a little bit in Google and found the following solution.

Copy A File To Multiple Directories In Command Line On Linux

I have text file called ostechnix.txt in my home directory, and I wanted to copy it to two different directories called Downloads and Documents in one go. It's not that difficult. Here is some simple methods to accomplish this task.

Method 1 - Using tee command

Tee is a Unix and Linux utility used to read from standard input and write to standard output and files.

Now, let us copy the ostechnix.txt file into two different directories called /home/sk/Downloads and /home/sk/Documents.

To do so, just run the following command from the Terminal:

$ tee /home/sk/Downloads/ostechnix.txt /home/sk/Documents/ostechnix.txt < /home/sk/ostechnix.txt

The above command will copy the ostechnix.txt file from home directory to Downloads and Documents directory.

Please note that tee command will also write the input to the standard output. If you don't want tee command to do this, just redirect the standard output to /dev/null as shown below.

$ tee /home/sk/Downloads/ostechnix.txt /home/sk/Documents/ostechnix.txt < /home/sk/ostechnix.txt >/dev/null

Please be mindful that if there is any file already present with the same same (i.e ostechnix.txt), the above commands will overwrite the existing file.

Method 2: Using find command

We can copy a single file to multiple directories at once using find command. It is a Unix and Linux command to search for files in a directory hierarchy.

If you want to copy ostechnix.txt file to Downloads and Documents directories, run the following command:

$ find Downloads/ Documents/ -maxdepth 0 -type d -exec cp ostechnix.txt {} \;

The above command will copy ostechnix.txt file to the specified directories.

If you don't specify '-maxdepth 0' option, the ostechnix.txt file will be copied to  Downloads, Documents directories and also into their sub-directories as well. So, don't forget to specify it.

That's it. There might be other ways to copy a file to multiple directories at once. But, I believe these commands are very simple and easy to use. If you're new to Linux, I always suggest you to test these commands in any test machines before start using them in the production. This will prevent the unnecessary loss of data.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like


zorg6 December 12, 2016 - 10:47 am

Keep It Short and Simple.

You can do it simply by :

for dest in Downloads Documents ; do cp ostechnix.txt “${dest}”; done

SK December 12, 2016 - 2:18 pm

Thank you! I never know this command.


Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More