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

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

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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 5 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

5 comments… add one
  • dipanjan mukherjee Jan 17, 2017 @ 3:48

    This is really helpful.

  • Amram Edry Jan 18, 2017 @ 1:08

    Thank you
    Great article

  • Ravi Kumar Chintalapudi Jan 18, 2017 @ 1:15

    Helpful information….Thank you.

  • Billy Bob Jan 18, 2017 @ 17:41

    Excellent note.
    Thanks.

  • Wai Htut Paing Jan 23, 2017 @ 9:33

    Very helpful , tips.
    Thanks

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum