How to Copy a File to Multiple Directories in Linux

While learning Linux, it is always the norm for newbies to keep on typing several commands to accomplish a simple task. This is understandable especially when one is just getting accustomed to using the terminal.

However, as you look forward to becoming a Linux power user, learning what I would refer to as “shortcut commands” can significantly reduce time wasting tendencies.

In this article, we will explain an easy way, using a single command to copy a file into multiple directories in Linux.

In Linux, the cp command is used to copy files from one directory to another, the easiest syntax for using it is as follows:

# cp [options….] source(s) destination

Alternatively, you can also use the advanced-copy command, which shows a progress bar while copying large files/folders in Linux.

Consider the commands below, normally, you would type two different commands to copy the same file into two separate directories as follows:

# cp -v /home/aaronkilik/bin/sys_info.sh /home/aaronkilik/test
# cp -v /home/aaronkilik/bin/sys_info.sh /home/aaronkilik/tmp
Copy Files to Multiple Directories
Copy Files to Multiple Directories

Assuming that you want to copy a particular file into up to five or more directories, this means you would have to type five or more cp commands?

To do away with this problem, you can employ the echo command, a pipe, xargs command together with the cp command in the form below:

# echo /home/aaronkilik/test/ /home/aaronkilik/tmp | xargs -n 1 cp -v /home/aaronkilik/bin/sys_info.sh

In the form above, the paths to the directories (dir1,dir2,dir3…..dirN) are echoed and piped as input to the xargs command where:

  1. -n 1 – tells xargs to use at most one argument per command line and send to the cp command.
  2. cp – used to copying a file.
  3. -v – enables verbose mode to show details of the copy operation.
Copy File to Multiple Locations in Linux
Copy File to Multiple Locations in Linux

Try to read through the man pages of cp, echo and xargs commands to find useful and advanced usage information:

$ man cp
$ man echo
$ man xargs

That’s all, you can send us questions in relation to the topic or any feedback through the comment form below. You may also want to read about the progress command which helps to monitor the progress of (cp, mv, dd, tar, etc.) commands that are presently running in Linux.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

15 thoughts on “How to Copy a File to Multiple Directories in Linux”

  1. Very interesting article, so in:

    # echo /home/aaronkilik/test/ /home/aaronkilik/tmp | xargs -n 1 cp -v /home/aaronkilik/bin/sys_info.sh

    the output from echo is appended to the “cp -v {source-file}”-command. What if we wanted to switch source and destination, so copy from multiple sources into a single destination? How to proceed then (preferably with a one-liner)? Something like copying sys_info1sh and sysinfo2.sh into the tmp-directory:

    ## echo /home/aaronkilik/test/sys_info1.sh /home/aaronkilik/tmp/sys_info2.sh | xargs -n 1 cp -v ?????????????? /home/aaronkilik/bin/

    So I/we need to take the output from echo and prepend (instead of appending it) to the “cp -v”-command – how do achieve this?

    Reply
  2. Nice article. I often have to move files to another system to multiple dir. I use SCP for that. e.g. scp /source/dir/file1 file2 file3 @:/target/dir

    Reply
  3. Thanks for the info! In turn, some for you: The article AN is used before singular, countable nouns which begin with vowel sounds. Thus “…an easy way…”
    Cheers

    Reply
  4. Nice article, I think xargs option “-n 1” – tells xargs to use 1 argument per command line at a time. (single line input to xargs has two whitespace separated arguments)

    Reply
  5. You aways come up with something great, I can just wait to see what you have for next week. I follow you all the time and this is just another example of content sharing.

    Reply
  6. an other (simpliest ?) solution :

    for dir in dir1 dir2 dir3
    do
    cp -v file1 file2 file3 $dir
    done

    for…do…done is basment of shell power

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.