Friday, February 3, 2017

[Quick Tips: Copy 1File to Multiple DIR]: How To Copy A File To Multiple Directories In Command Line On Linux

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

Today, 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 this solution. In this brief guide, I will be explaining how to copy a file to multiple directories in command line on Linux.

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 copied 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.
Let us see how to use this command to accomplish this task.
find Downloads/ Documents/ -type d -exec cp ostechnix.txt {} \;
The above command will copy ostechnix.txt file into  Downloads, Documents directories and also into their sub-directories as well.
If you only want to copy ostechnix.txt file to Downloads and Documents directories, and not in their sub-directories, run the following command with -maxdepth 0 option :
find Downloads/ Documents/ -maxdepth 0 -type d -exec cp ostechnix.txt {} \;
The above command will copy ostechnix.txt file to the specified directories, not in their sub-directories.
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.

No comments: