How to copy a single file to multiple directories in Linux or Unix

Posted on in Categories , , , , , , , , last updated January 17, 2017

If I want to copy a single file into three locations (directories), by using a single cp command (e.g. cp file /dir1/ /dir2/ /dir3/. Would that be possible? If yes, please provide the command for GNU/Linux or Unix operating systems.

The short answer is no. You can not use GNU/cp or BSD/cp to copy a single file to multiple directories. However, you can use combination of cp and xargs/parallel and other commands to copy a single file to multiple directories in MacOS, Linux, FreeBSD, OpenBSD, NetBSD and Unix-like systems.

Copying one file to multiple directories

The cp command

The basic cp command syntax is as follows to copy multiple files to a single directory:
cp file1 /dir1/
cp file1 file2 file3 /dir1/

Copy file to multiple directories in Unix or Linux

The trick is to use the xargs command or GNU parallel command.

Copying one file named foo.txt to multiple directories called /tmp/1/, /tmp/2/, and /tmp/3 using xargs

The xargs command construct argument list(s) and execute utility such as cp or mv. The syntax is:

xargs -n 1 cp -v filename<<<"/dir1/ /dir2/ /dir3/" 
 
## OR ##
echo "/dir1/ /dir2/ /dir3/" | xargs -n 1 cp -v filename

In this example copy /etc/passwd to /tmp/1/, /tmp/2/, and $HOME/3/ as follows:
xargs -n 1 cp -v /etc/passwd<<<"/tmp/1/ /tmp/2/ $HOME/3/"
OR
echo "/tmp/1/ /tmp/2/ $HOME/3/" | xargs -n 1 cp -v /etc/passwd
Sample outputs:

Fig.01: Copy multiple files in different directories using the Linux/Unix cp command
Fig.01: Copy multiple files in different directories using the Linux/Unix cp command

Copying one file named foo.txt to multiple directories called /tmp/1/, /tmp/2/, and /tmp/3 using parallel

GNU parallel is a shell tool for executing jobs in parallel using one or more computers. The syntax is:

parallel cp fileNameHere ::: /dir1/ /dir2/ /dir3/

In this example copy /etc/resolv.conf to /tmp/1/, /tmp/2/, and $HOME/3/ as follows:
parallel cp -v /etc/resolv.conf ::: /tmp/1/ /tmp/2/ $HOME/3/
Sample outputs:

/etc/resolv.conf -> /tmp/2/resolv.conf
/etc/resolv.conf -> /tmp/1/resolv.conf
/etc/resolv.conf -> /Users/vivek/3/resolv.conf

How to use tee command to copy file to multiple folders

Say you want to copy /etc/resolv.conf to /tmp/1/ and /tmp/2/, you run the tee command as follows:
tee /tmp/1/resolv.conf /tmp/2/resolv.conf < /etc/resolv.conf tee /tmp/1/resolv.conf /tmp/2/resolv.conf < /etc/resolv.conf >/dev/null
You need to specify full path like /tmp/1/resolv.conf, otherwise you get the following error:

tee: /tmp/1/: Is a directory

How to use find command to copy files to multiple dirs simultaneously

The syntax is as follows for the find command:
find /dir1/ /dir2/ -maxdepth 0 -exec cp filename {} \;
To copy /etc/hosts simultaneously to /tmp/{1,2}/ and $HOME/3/ folders under a Linux/Unix/MacOS/FreeBSD etc, enter:

find /tmp/1/ /tmp/2/ $HOME/3/ -maxdepth 0 -exec cp /etc/hosts {} \;

Sample outputs:

/etc/hosts -> /tmp/1/hosts
/etc/hosts -> /tmp/2/hosts
/etc/hosts -> /Users/vivek/3/hosts

Shell loop

You can also use bash for loop as follows:

for dest in /dir1/ /dir2/ ; do cp -v file "$dest" ; done
for dest in /tmp/1/ /tmp2/ $HOME/3/ ; do cp -v "/etc/resolv.conf" "$dest" ; done

5 comment

Leave a Comment