Skip to content

How to Copy a File to Multiple Directories in Linux

Category
Linux

To copy a file to multiple directories in Linux, you can use the `cp` command with the `xargs` command. Here all the destination directories will be piped as standard input to the `xargs` command. and specify a list of directories separated by space. For example:

echo dir1 dir2 dir3 | xargs -n 1 cp -v file.txt 

This will copy the file file.txt to the directories dir1, dir2, and dir3.

Alternatively, you can use the for loop to copy the file to multiple directories. For example:

for dir in dir1 dir2 dir3; do
    cp file.txt $dir
done

This will loop through the list of directories and copy the file to each of them.

You can also use the find command to copy the file to multiple directories. For example:

find dir1 dir2 dir3 -type d -exec cp file.txt {} \; 

This will find all directories named dir1, dir2, and dir3 and copy the file to each of them.

Note: Make sure you have the necessary permissions to copy the file to the target directories.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

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


Back to top