Skip to content
Related Articles

Related Articles

How to Copy a File to Multiple Directories in Linux

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 23 Aug, 2021

in this article, we will discuss how to copy a file to multiple directories in Linux. xargs, find, tee, shell loop, and parallel are the commands that can help you to copy a File to Multiple Directories.

Suppose we have a file with the name “xyz.txt” and we want to copy it into 3 directories with the name dir1,dir2, and dir3 than we use the following methods:

Using xargs

xargs command on Unix/Linux operating system converts input from standard input into an argument list for a specified command.


xargs -n 1 cp -v xyz.txt<<<"dir1 dir2 /home/kalilinux/dir3" 


echo "dir1 dir2 /home/kalilinux/dir3" | xargs -n 1 cp -v xyz.txt


In the second command, working will be the same as the first one but the target folders and directory(dir1,dir2,dir3) are first echoed and then fed as input to the command xargs.


  • -n 1:  It is instructing xargs to use one argument per command line at a time.
  • cp: It is basic copy command.
  • -v: Display informative messages as the copy is performed.
  • xyz.txt: It is the file name that we want to copy.
  • dir1 dir2 are the folders name and /home/kalilinux/dir3 is the directory name with its proper location and these folders and directory are the destinations where to copy that file.
  • Using the pipe operator |, the standard output of one command can be sent into the standard input of another.

Note: If the file to be copied exists already in one of the destination folders, the old file will be replaced without prompting the user.

It is compulsory to specify the full path for the directories otherwise you get the following message:

You need to be in that directory where the folders or files are present for directly copying to that folder or file and don’t need to specify its full path or location. If you specify the full location of the file or folder then no matter where you are, you won’t get any error.


Using find

Here, the command find initiates a search and allows actions to be performed based on the search results.


find dir1 dir2 /home/kalilinux/dir3 -maxdepth 0 -exec cp xyz.txt {} \;


  • dir1 dir2 are the folders name and /home/kalilinux/dir3 is the directory name with its proper location where we want to perform the copy action.
  • -maxdepth: Set the maximum number of levels(like here we have set 0) that find will descend into a directory tree when performing tests and actions.
  • -exec: It is used to perform User-defined actions. In addition to the predefined actions, we can also invoke arbitrary commands with the help of the -exec command.
  • cp: It is the basic copy command.


Using Shell loop

Here, we are using bash for loop to copy the xyz.txt file in the /home/kalilinux/dir2 /home/kalilinux/dir3 directories and also a folder dir1, directly where:

  • for: It illustrating the folders and directories.
  • dest: It is used to creates a stream for writing metadata objects to the file system.
  • dir1: It is the folder 
  •  /home/kalilinux/dir2  /home/kalilinux/dir3: These are the directories that are separated by the spaces that we give between them.
  • do: It is executing the cp command.
  • -v: It is used to display informative messages as the copy is performed.
  • xyz.txt‘: It is the file name that we want to copy in the folder and directories.
  • done: is used to end the shell script.


for dest in dir1 /home/kalilinux/dir2 /home/kalilinux/dir3 ; do cp -v xyz.txt “$dest” ; done


 Using tee

The tee command reads standard input and copies it to both standard outputs and to one or more files.


tee /home/kalilinux/dir1/xyz.txt /home/kalilinux/dir2/xyz.txt /home/kalilinux/dir3/xyz.txt< xyz.txt

If you just want to read standard input and copies it into one or more files without showing the standard output then you can use >/dev/null at the end of the above command. For example:

tee /home/kalilinux/dir1/xyz.txt /home/kalilinux/dir2/xyz.txt /home/kalilinux/dir3/xyz.txt< xyz.txt >/dev/null


Note: It is a must to give any file name at the end of the destination after giving the directories location. It can be different from the name of the file that we copy into the directories or can be the same as the file name.

Using GNU parallel

First, you need to install this on your pc/laptop because it may not be pre-installed in your Linux operating systems. You can install it by using the command :

sudo apt install parallel

then after we use:

parallel cp -v xyz.txt ::: /home/kalilinux/dir1 /home/kalilinux/dir2 dir3

GNU Parallel is a shell utility for executing jobs in parallel which makes running the command against sets of data at the same time easier. Here we are using:

  • parallel: This keyword uses the GNU Parallel.
  • cp: to perform the copy task.
  • -v: to display informative messages as the copy is performed.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!