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:

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

-n 1 – tells xargs to use at most one argument per command line and send to the cp command.

cp – used to copying a file.

-v – enables verbose mode to show details of the copy operation.

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.

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.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

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?