copy file to multiple directories in linux

How to Copy File to Multiple Directories in Linux

Sometimes you may need to copy file to multiple folders & subfolders in Linux. In this article, we will look at how to copy file to multiple directories. You can use these commands in almost every Linux distribution.


How to Copy File to Multiple Directories in Linux

We will basically use cp command to copy a file to multiple folders. However cp allows you to copy one or more files to a single destination folder, and not the other way round.

So we will have to use another way to copy single file to multiple locations. Here are the different ways to do it. Please use the full paths to your file and destination folders in all the following commands. Otherwise, Linux will look for them in your current working directory.


Using xargs

xargs allows you to loop through a set of arguments (destination folders) and auto-generate the cp command for each destination folder. Here is an example to copy one file to two folders. You can include more directories in your echo command, if you want.

$ sudo echo /home/ubuntu/folder1 /home/folder2 | xargs -n 1 -v cp /home/data.txt

In the above case, the echo command’s output is piped to xargs command. The -n1 option tells xargs to use 1 argument for each command it generates. So xargs will generate and run the following commands.

$ cp /home/data.txt /home/ubuntu/folder1
$ cp /home/data.txt /home/folder2


Using Find

Similarly Find command also allows you to copy file to multiple directories. Here is the above command written using find.

$ sudo find /home/ubuntu/folder1 /home/folder2 -exec cp /home/data.txt {} \;

If the above destination folders also have subfolders, and you do not want to copy files to those subdirectories, then use -maxdepth 0

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


Using Shell Loop

You may also write a simple shell script to copy single file to multiple files. Open terminal and run the following command to create an empty shell script

$ sudo vi copy_multiple.sh

Add the following lines to it. Replace /home/file.txt with the full path to your file.

#!/bin/bash

FILE = "/home/file.txt"

for f in "$@"
do
  sudo cp $FILE "$f"
done

Save and close the file. In the above code, $@ contains all the folder paths entered via command line arguments. Our shell script basically loops through the folder paths one by one, and in each iteration, copies our file, whose path is saved in $FILE, to each folder. If you want to recursively copy the file to the subfolders of each folder, use -rf option in your cp command in the above script.

Make the shell script executable

$ sudo chmod +x copy_multiple.sh

Run the script as shown below.

$ sudo ./copy_multiple.sh /etc/ubuntu /home/project /home/data

That’s it. We have seen three different ways to copy single file to multiple folders and directories.

Also read:

Shell Script to Loop Through Files in Directory
How to Loop Over Lines of File in Bash
Shell Script to Tar Multiple Files & Directories
How to Check if Directory Exists in Shell Script
Shell Script to Delete Files in Directory

Leave a Reply

Your email address will not be published. Required fields are marked *