How to copy a file to multiple directories in Linux

In this short article, you will learn how to copy one file into many directories. The obvious answer would be to use the cp command but this not is exactly correct since the cp command is rather used to copy several files into one directory. You will see what additional commands and combinations are needed to pull this off. Let’s get started.

The cp command basic syntax

The most basic cp command syntax that is used to copy multiple files into one directory is the following :

cp file_1 /directory_1/

cp file_1 file_2 file_3 /directory_1/

Here is an example :

cp /Documents/FileExample.txt /TextFiles/

cp command

How to copy one file to several directories ?

As mentioned above, the cp command cannot be used to copy one file to multiple folders. The solution would be to use the xargs or GNU parallel commands.

Using xargs

To copy for instance file_1 into the folders directory_1, directory_2, we could proceed as follows:

echo directory_1 directory_2 | xargs -n 1 cp file_1

In the command above, target directories (directory_1,directory_2) are first echoed and then piped out (or fed) as input to the command xargs where:

-n 1 : Instructs xargs to use one argument per command line at a time and forward to the cp command

cp : classic cp command

-v : Allows verbose mode in order to display more details of the copy task

The xargs will execute the cp command two times (i.e. as many target directories as provided in the input) where at each run, it appends the next directory path fed to it from the previous echo command to the end of the standard cp command.
So instead of executing two separate cp commands, we can now use one single command to perform the same task. If the file to be copied exists already in one of the destination folders, the old file will be replaced without prompting the user. In other words, the -i option (interactive) of the cp command cannot be used in conjunction with xargs.
If the file to be copied has a large size and you do not want the destination file (if it exists) to be replaced, you might want to add the -n switch to the cp command in the single line above. This will prevent the destination file from being replaced.

Read: How to use grep command in Linux


Using find

Another alternative to carrying out a copy to multiple destinations is to use the find command as follows :

find directory1 directory2 -exec cp file.txt {} \;

If the target directories have sub-directories and you don’t want to copy the file into them, you would need to add -maxdepth 0 option as follows:

find directory1 directory2 -maxdepth 0 -exec cp file.txt {} \;

This will overwrite or replace every file in directory1 and directory2 with the content of file.txt before copying it. In order not to affect other files in these destination directories, make find aware that it should only act on directories as follows:

find directory1 directory2 -type d -exec cp file.txt {} \;

Read: Linux directories explained

Using loop in a shell

xfAnother solution would be to use a for loop within a one line shell as follows :

for dir in *; do [ -d “$dir” ] && cp /full_path/file.txt “$dir” ; done

This will copy the file /full_path/file.txt to all directories in your current path or location.

for dir in *; do [ -d “$dir” ] && cp -rf /full_path/folder “$dir” ; done

This however will copy the folder /full_path/folder to every sub-folder or sub-directory in your current location.

Read: How to find the largest files on Linux

Using GNU parallel

GNU parallel is a shell utility used for executing tasks or jobs in parallel over one or multiple machines. The basic syntax is as follows:

parallel cp file_name ::: /directory1/ /directory2/

Here is an example on how to use it :

Let’s copy the file /etc/resolv.conf to /directory1/, /directory2/ :

parallel cp -v /etc/resolv.conf ::: /directory1/, /directory2/

Using tee

The tee command allows you to copy one file to multiple destinations. Here is an example on how to perform this:

tee ~/directory1/file1 ~/directory2/file1 < ~/file1

Note that the input that is written by tee will be forwarded to the standard output (stdout). If you don’t like this behavior, you have the possibility to prevent it by rerouting standard output to /dev/null as shown below:

tee ~/directory1/file1 ~/directory2/file1 < ~/file1 >/dev/nul

Conclusion

As you have seen, there are many ways to copy a file to multiple directories. Most of these solutions make use of the cp command which cannot perform this feat on its own.

amin nahdy

Amin Nahdy, an aspiring software engineer and a computer geek by nature as well as an avid Ubuntu and open source user. He is interested in information technology especially Linux based ecosystem as well as Windows and MacOS. He loves to share and disseminate knowledge to others in a transparent and responsible way.

This Post Has 4 Comments

  1. On my system (KDE neon) using parallel from the moreutils package, you need to use — as separator, not :::.

    1. Duly noted – thanks for pointing this out

  2. cp -r

    Directories won’t be copied without the recursive option

    1. Yes this is by design – since there could be several files in a directory, the cp command doesn’t know in advance if it should copy them or just the directory …

Leave a Reply

Close Menu