Open In App

How to Copy a File to Multiple Directories in Linux

Last Updated : 05 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 then 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.

Syntax:

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

                        OR

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

Output:

Using xargs

Using xargs

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.

Output:

Using xargs

Using 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:

xargs

xargs 

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 errors.

Example:

 

Using find

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

Syntax:

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

Were,

  • 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.

Output:

Using find

Using find

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 is illustrating the folders and directories.
  • dest: It is used to create 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.

Syntax:

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

Output:

Using Shell loop

Using Shell loop

 Using tee

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

Syntax:

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 copy 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

Output:

Using tee

Using tee

Note: It is a must to give any file name at the end of the destination after giving the directory’s 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.

Output:

Using GNU parallel

Using GNU parallel

Conclusion

In this article we have discussed how to copy a file to multiple directories in Linux and how it can be achieved using various methods, such as utilizing the `cp` command with brace expansion, leveraging `find` and `xargs`. Each method provided flexibility and efficiency in different scenarios. Overall, we can say that after understanding these methods and adapting them to our specific requirements, we can save time and effort when dealing with the directories in Linux.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads