How To Copy A File To Multiple Directories in Linux

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

(https://net2.

com/)

Home (Https://Net2.Com/) Linux (Https://Net2.Com/Category/Linux/)

Linux Tips & Tricks (Https://Net2.Com/Category/Linux/Ubuntu_tips_and_tricks/)

Maintenance And Monitoring (Https://Net2.Com/Category/Maintenance-And-

Monitoring/)

Installations (Https://Net2.Com/Category/Installations/)

Troubleshooting (Https://Net2.Com/Category/Troubleshooting/)

Best Apps (Https://Net2.Com/Category/Best-Apps/) 

SEARCH
How to copy a file to
Search
multiple directories in
Linux RECENT POSTS

How to use the screen


 amin nahdy (https://net2.com/author/aminn2000/) -  January 2, 2020 - command on Linux to
 Debian (https://net2.com/category/linux/debian/) / Linux
(https://net2.com/category/linux/) / Linux Mint keep your remote task
(https://net2.com/category/linux/linux_mint/) / Ubuntu running when the
(https://net2.com/category/linux/ubuntu/) -
 4 Comments (https://net2.com/how-to-copy-a-file-to-multiple-directories-in- connection drops
linux/#comments) (https://net2.com/how-to-
use-the-screen-command-
In this short article, you will learn how to copy one file into many on-linux-to-keep-your-
directories. The obvious answer would be to use the cp command but remote-task-running-
this not is exactly correct since the cp command is rather used to copy when-the-connection- 
drops/) January 5, 2020
several files into one directory. You will see what additional commands How to copy a file to
and combinations are needed to pull this off. Let’s get started. multiple directories in
Linux
The cp command basic syntax (https://net2.com/how-to-
copy-a-file-to-multiple-
The most basic cp command syntax that is used to copy multiple files directories-in-linux/)
into one directory is the following : January 2, 2020

How to kill a process in


cp file_1 /directory_1/
Linux – guide for
beginners
cp file_1 file_2 file_3 /directory_1/
(https://net2.com/how-to-
kill-a-process-in-linux-
Here is an example :
guide-for-beginners/)

cp /Documents/FileExample.txt /TextFiles/ December 31, 2019

How to save power on


Linux Ubuntu/Debian
using cpufreq
(https://net2.com/how-to-
save-power-on-linux-
How to copy one file to several directories ?
ubuntu-debian-using-

As mentioned above, the cp command cannot be used to copy one file cpufreq/) December 30,
2019
to multiple folders. The solution would be to use the xargs or GNU
parallel commands. How to run Windows
applications on Linux
Using xargs (https://net2.com/how-to-
run-windows-applications-
To copy for instance file_1 into the folders directory_1, directory_2, we on-linux/) December 27,
could proceed as follows: 2019

echo directory_1 directory_2 | xargs -n 1 cp file_1


MOST POPULAR
POSTS
In the command above, target directories (directory_1,directory_2) are
first echoed and then piped out (or fed) as input to the command xargs How to fix WiFi not
where: working on Ubuntu
(https://net2.com/how-to-
-n 1 : Instructs xargs to use one argument per command line at a time fix-wifi-not-working-on-
and forward to the cp command ubuntu/)
How to copy a file to
cp : classic cp command multiple directories in
Linux
-v : Allows verbose mode in order to display more details of the copy How to Fix Unmet
task Dependencies Error on
Ubuntu 
The xargs will execute the cp command two times (i.e. as many target (https://net2.com/how-to-
directories as provided in the input) where at each run, it appends the fix-unmet-dependencies-

next directory path fed to it from the previous echo command to the error-in-ubuntu/)
How to Google search
end of the standard cp command.
from Linux terminal –
So instead of executing two separate cp commands, we can now use
Build your own search
one single command to perform the same task. If the file to be copied
engine
exists already in one of the destination folders, the old file will be
(https://net2.com/how-to-
replaced without prompting the user. In other words, the -i option
google-search-from-linux-
(interactive) of the cp command cannot be used in conjunction with terminal-build-your-own-
xargs. search-engine/)
If the file to be copied has a large size and you do not want the How to fix high memory
destination file (if it exists) to be replaced, you might want to add the -n usage in Linux
switch to the cp command in the single line above. This will prevent (https://net2.com/how-to-

the destination file from being replaced. fix-high-memory-usage-in-


linux/)

Read: How to use grep command in Linux Best download managers


for Linux Ubuntu/Debian
(https://net2.com/how-to-use-grep-command-in-linux/)
(https://net2.com/best-
download-managers-for-
linux-ubuntu-debian/)
How to speed up Linux
Using find (https://net2.com/how-to-
speed-up-linux/)
Another alternative to carrying out a copy to multiple destinations is to
How to fix Ubuntu boot
use the find command as follows :
issues
(https://net2.com/how-to-
find directory1 directory2 -exec cp file.txt {} \;
fix-ubuntu-boot-issues/)
How to merge or split PDF
If the target directories have sub-directories and you don’t want to copy
files on Linux
the file into them, you would need to add -maxdepth 0 option as
(https://net2.com/how-to-
follows:
merge-or-split-pdf-files-on-
linux/)
find directory1 directory2 -maxdepth 0 -exec cp
How to list, start and stop
file.txt {} \; services at boot time in
Linux Ubuntu/Debian
This will overwrite or replace every file in directory1 and directory2 with
(https://net2.com/how-to-
the content of file.txt before copying it. In order not to affect other files list-start-and-stop-
in these destination directories, make find aware that it should only act services-at-boot-time-in-
on directories as follows: linux-ubuntu-debian/)

find directory1 directory2 -type d -exec cp


file.txt {} \;


Read: Linux directories explained (https://net2.com/linux-

directories-explained/)

Using loop in a shell (https:

xfAnother solution would be to use a for loop within a one line shell as //www
follows :

 .faceb
for dir in *; do [ -d “$dir” ] && cp
/full_path/file.txt “$dir” ; done
(https: ook.co
This will copy the file /full_path/file.txt to all directories in your current
path or location. //twitt m/ami

for dir in *; do [ -d “$dir” ] && cp -rf


/full_path/folder “$dir” ; done
er.com nnahd

This however will copy the folder /full_path/folder to every sub-folder /Amin y.net2
or sub-directory in your current location.

_net2) /)
Read: How to find the largest files on Linux
(https://net2.com/how-to-find-the-largest-files-on-linux/) 

Using GNU parallel (https:

GNU parallel is a shell utility used for executing tasks or jobs in parallel
//www
over one or multiple machines. The basic syntax is as follows:

parallel cp file_name ::: /directory1/ /directory2/ .pinter

Here is an example on how to use it :


est.co
Let’s copy the file /etc/resolv.conf to /directory1/, /directory2/ :
m/Am
parallel cp -v /etc/resolv.conf ::: /directory1/,
/directory2/ in_net

Using tee
2/)
The tee command allows you to copy one file to multiple destinations.
Here is an example on how to perform this: STAY INFORMED BY
JOINING OUR
NEWSLETTER!
tee ~/directory1/file1 ~/directory2/file1 < ~/file1

Note that the input that is written by tee will be forwarded to the
Email
standard output (stdout). If you don’t like this behavior, you have the
possibility to prevent it by rerouting standard output to /dev/null as SUBSC R I B E
shown below:

tee ~/directory1/file1 ~/directory2/file1 < ~/file1


>/dev/nul

Conclusion

As you have seen, there are many ways to copy a file to multiple
directories. Most of these solutions make use of the cp command
which cannot perform this feat on its own.

TAGS: PARALLEL COMMAND (HTTPS://NET2.COM/TAG/PARALLEL-COMMAND/), TEE


COMMAND (HTTPS://NET2.COM/TAG/TEE-COMMAND/), XARGS COMMAND
(HTTPS://NET2.COM/TAG/XARGS-COMMAND/)

 Previous Post Next Post 


How to kill a process in How to use the screen
Linux – guide for beginners command on Linux to keep
(https://net2.com/how-to-kill-a- your remote task running
process-in-linux-guide-for- when the connection drops
beginners/) (https://net2.com/how-to-use-
the-screen-command-on-linux-
to-keep-your-remote-task-
running-when-the-connection-
drops/)

4 Leave a Reply

Join the discussion...

2  2  0     3 

  Subscribe    newest  oldest  most voted

Deryk Barker  

On my system (KDE neon) using parallel from the


Guest moreutils package, you need to use — as separator,
not :::.

 0    Reply
 1 day ago | View Replies (1) 

Major  

cp -r
Guest
Directories won’t be copied without the recursive
option

 0    Reply
 1 day ago | View Replies (1) 

Join net2@Reddit
(https://reddit.com/r/net
2)
Copyright net2.com all rights reserved 2019 contact
about convert pdf to
| word policy/)
us (https://net2.com/contact-us/)
(https://net2.com/about/) for| free
(https://net2.com/convert-pdf-to-word/) |
privacy policy (https://net2.com/privacy-

You might also like