How to Copy a File to Multiple Directories in Linux

23 Aug, 2021
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 than we use the following methods:
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:
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:
-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:
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 error.
Example:
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 {} \;
Where,
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:
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 illustrating the folders and directories.
dest: It is used to creates 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:
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 copies 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:
Note: It is a must to give any file name at the end of the destination after giving the directories 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.
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:
linux-command
Linux-file-commands
Picked
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Docker - COPY Instruction
scp command in Linux with Examples
chown command in Linux with Examples
SED command in Linux | Set 2
Introduction to Linux Operating System
nohup Command in Linux with Examples
Array Basics in Shell Scripting | Set 1
mv command in Linux with examples
chmod command in Linux with examples
screen command in Linux with Examples