How to Copy a File to Multiple Directories in Linux?

LinuxOperating SystemFile System

Did you get to take one file on a Linux or Unix approach and replicate it to a whole bunch of alternative directories? Then, this article is for you to copy a file to multiple directories in Linux/Ubuntu.

Using with cp and xargs

To copy a file to multiple directories in Linux/Ubuntu, use the following in command –

$ echo dir1 dir2 dir3 | xargs -n 1 cp file1

In the above command, we are copying file1 to dir1,dir2, and dir3 directories.The sample example of the above command is as shown below −

$ echo Music Videos Desktop | xargs -n 1 cp httpstat.py

In the above command, we are copying httpstat.py file to Music, Videos, and Desktop directories. To verify the above command, use the following command –

$ cd Music
~/Music$ ls

The sample output should be like this –

httpstat.py

Another sample to verify the above command is as shown below –

$ cd Desktop
~/Desktop$ ls

The sample output should be like this –

httpstat.py

Using with cp and find

To copy a file to multiple directories in Linux/Ubuntu, use the following in command –

find dir1 dir2 dir3 -type d -exec cp file1 {} \;

The above command finds dir1,dir2, and dir3 directories and copy file1 to those directories.The sample example is as shown below –

$ find Music Desktop -type d -exec cp httpstat.py {} \;

In the above command, we are copying httpstat.py file to Music and Desktop directories. To verify the above command, use the following command –

$ cd Desktop
~/Desktop$ ls

The sample output should be like this –

httpstat.py

Another sample to verify the above command is as shown below –

$ cd Music
~/Music$ ls

The sample output should be like this –

httpstat.py

In the above article, we have learnt – How to Copy a File to multiple directories in Linux. In our next articles, we will come up with more Linux based tricks and tips. Keep reading!

raja
Published on 28-Jan-2020 14:41:51
Advertisements