Home Linux Commands Copy A File To Multiple Directories From Command Line On Linux

Copy A File To Multiple Directories From Command Line On Linux

By sk
16.9K views

In this brief guide, we will discuss how to copy a file to multiple directories from command line using find, cp, echo, xargs and tee commands on Linux.

The other day I wanted to copy some videos to different folders in my Arch Linux desktop. As you already know, we can easily do it by right-clicking on the file, select Copy option from the context menu and paste it on the destination directory/folder.

However, I'd like to know if there are any other way 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 did a few web searches and come up with following solutions.

Important Warning:

Please note that the following commands will not ask any confirmation. If there is any file exists with the same name on the target directory, it will be replaced automatically.

If you're new to Linux, I suggest you to test the commands in any test machines before start using them in the production. This way you can prevent the unnecessary loss of data.

Copy a file to multiple directories from 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.

First, let us see how to copy the file to multiple different directories using find and cp commands.

Method 1: Using find and cp commands

We can copy a single file to multiple directories at once using find and cp commands.

The find is a Unix and Linux command to search for files in a directory hierarchy and the cp command is used to copy files and directories.

To copy a file named ostechnix.txt to ~/Downloads and ~/Documents directories using find and cp command, run:

$ find ~/Downloads/ ~/Documents/ -maxdepth 0 -type d -exec cp ostechnix.txt {} \;
Copy a file to multiple different directories using find command in Linux
Copy a file to multiple different directories using find and cp command in Linux

The above command will copy ostechnix.txt file in the current directory to the ~/Downloads and ~/Documents 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.

As I warned earlier, if there is any file already present with the same name (i.e ostechnix.txt) in any of the destination directories, the above command will simply overwrite the existing file without any confirmation.

If you do not want to overwrite the existing file, you can use -n flag with cp command like below:

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

Now, it will not replace the target file if it already exists in the destination locations.

Method 2: Using echo, xargs and cp commands

The another way to copy files to multiple locations is by using echo, xargs and cp commands.

As you already know, the cp command is used to copy the files and directories, and xargs command is used to build and execute command lines from standard input.

To copy a file to multiple directries using echo, xargs and cp commands, run:

$ echo ~/Downloads ~/Documents | xargs -n 1 cp ostechnix.txt
Copy a file to multiple directories using cp, echo and xargs commands in Linux
Copy a file to multiple directories using cp, echo and xargs commands in Linux

Let us break down the above command and see how it works.

Generally, the echo command prints the given string or text to the standard output. But in this case, we pipe the output of the echo command to xargs. Here, echo command will send the directory path to xargs as input.

xargs command runs the cp command two times and appends the path of each directory on to the end of the cp command.

The xargs commands receives two arguments (i.e. directory path) from echo command. The -n 1 option on the xargs command tells the xargs to only append one of those arguments at a time.

Again, the above command replaces the target file it it already exists. If you don't want to replace the target file, add -n flag in the command:

$ echo ~/Downloads ~/Documents | xargs -n 1 cp -n ostechnix.txt

Method 3 - 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
Copy a file to multiple different directories using tee command in Linux
Copy a file to multiple different directories using tee command in Linux

Again I warn you. If there is any file already present with the same name (i.e ostechnix.txt) in the destinations, the above command will overwrite the existing file.

For details, check the manaul pages of the above commands:

$ man cp
$ man find
$ man xargs
$ man echo
$ man tee

Conclusion

This is how we copy a file to multiple directories at once from command line in Linux. There could be other ways, but I believe these commands are very simple and easy to use.

Again, make sure the destination paths are correct and most imporantly make sure that there is no file exists with the same name in the target location.

You May Also Like

2 comments

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

Reply
SK December 12, 2016 - 2:18 pm

Thank you! I never know this command.

Reply

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. By using this site, we will assume that you're OK with it. Accept Read More