Copy a File To Multiple Directories With A Single Command on Linux

Copy A File To Multiple Destination In Linux

In Linux, there are times when you need to copy a file to multiple directories. When we think of copying the obvious command a seasoned Linux would filter it down to would be the versatile cp command. Here’s a catch though, it just won’t work for copying a single file to multiple destinations. Let’s look at

How the cp command fails?

The most basic way to copy a file to multiple directories is to use the cp command. For example, if you wanted to copy the file /tmp/file.txt to the directories /home/user1 and /home/user2, you would use the following command:

cp /tmp/file.txt /home/user1 /home/user2

Let’s test:

Screenshot 2022 09 28 At 12.25.32 PM
Screenshot of both GUI as well as Terminal which displays 3 directories and a file has been created to test cp command.

Executing the cp command to copy to multiple directories, I have taken the liberty to use -r command to copy the files recursively.

Screenshot 2022 09 28 At 12.27.54 PM
Screenshot of the cp command in action to copy a file to multiple directories
Screenshot 2022 09 28 At 12.30.25 PM
Well, the ‘cp’ command did not do its magic after all, what do you know!

Since the cp command did not work, let’s explore other ways to complete this task.

Using xargs to copy to multiple directories

One way to copy a file to multiple directories is to use the xargs command. This command takes a list of arguments and executes a command for each argument.

For example, if you wanted to copy the file /tmp/file.txt to the directories /home/user1 and /home/user2, you would use the following command:

echo /home/user1 /home/user2 | xargs -n 1 cp /tmp/file.txt

How xargds works

The xargs command is used to build and execute command lines from standard input. In the above command, the xargs command builds and executes the cp command line by line from the standard input.

The -n 1 option tells xargs to use only one argument from the standard input. Therefore, the first line of the standard input is used as an argument for the first cp command, and the second line of the standard input is used as an argument for the second cp command.

Screenshot 2022 09 28 At 12.44.41 PM
Executing the xargs command

By default, the files won’t copy, you will have to change the folder permissions using the chmod command if you are using Ubuntu.

sudo chmod -R 777 /home/
Screenshot 2022 09 28 At 12.49.04 PM
File successfully copied to multiple directories using the xargs command

Using find to copy a file to multiple directories at once

You can also use the find command to copy a file to multiple directories. The find command will search for files in a directory and then execute a command for each file.

For example, if you wanted to copy the file /tmp/file.txt to the directories /home/user1 and /home/user2, you would use the following command:

find /home/user1 /home/user2 -type d -exec cp /tmp/file.txt {} \;

How the find command helps

The find command is used to search for files in a directory hierarchy. The -type d option tells find to search for only directories. The -exec cp command tells find to execute the cp command for each directory that it finds. The {} symbols are replaced by the name of the directory that is being processed. The \; symbols end the -exec cp command.

Screenshot 2022 09 28 At 12.58.26 PM
File successfully copied to multiple directories using the find command

Using a loop in a shell to copy file to multiple directories

If you are using a shell script, you can use a loop to copy a file to multiple directories. For example, if you wanted to copy the file /tmp/file.txt to the directories /home/user1 and /home/user2, you would use the following command:

for i in /home/dir1 /home/dir2 /home/dir3; do cp /home/file.txt $i; done

How loops in shell scripts help

The for loop iterates through a list of values, executing a block of commands once for each value in the list. In the above command, the for loop iterates through the /home/dir1, /home/dir2, and /home/dir3 directories. For each directory, the cp command is executed, copying the /home/file.txt file to the directory.

Screenshot 2022 09 28 At 1.04.11 PM
File successfully copied to multiple directories by using loop in the shell

Using GNU Parallel

GNU parallel is a tool that can be used to run multiple commands in parallel. This can be useful for copying a file to multiple directories.

Do note that the command won’t be installed in your system by default. You can install it by running the apt command:

sudo apt install parallel

For example, if you wanted to copy the file /tmp/file.txt to the directories /home/user1 and /home/user2, you would use the following command:

parallel cp /tmp/file.txt ::: /home/user1 /home/user2

How the parallel app works

The parallel command is used to execute commands in parallel. In the above command, the parallel command executes the cp command in parallel for each of the arguments /home/user1 and /home/user2. Therefore, two cp processes will be run in parallel, each copying the file /tmp/file.txt to the respective home directory.

Screenshot 2022 09 28 At 1.10.05 PM
File successfully copied to multiple directories by using GNU parallel

Using tee

The tee command can be used to copy the output of a command to multiple files. This can be useful for copying a file to multiple directories.

For example, if you wanted to copy the file /tmp/file.txt to the directories /home/user1 and /home/user2, you would use the following command:

cp /tmp/file.txt | tee /home/user1/file.txt /home/user2/file.txt

How the tee command works

The tee command is used to read from standard input and write to standard output and one or more files. In the above command, the tee command reads from the /home/file.txt file and writes to the standard output and the /home/dir1/file.txt, /home/dir2/file.txt, and /home/dir3/file.txt files.

Screenshot 2022 09 28 At 1.21.39 PM
File successfully copied to multiple directories by using tee command

Summary

In this article, we have shown you some of the most common methods for copying a file to multiple directories in Linux using the tee, parallel, running loop in shell, find and xargs commands.