How to Copy a File to Multiple Directories With One Command in Linux

Cave-Man

Kıdemli Üye
21 Nis 2015
3,120
0
Lüksemburg

9gG9gO.png


Copying a file with the Linux command line is easy. However, what if you want to copy the same file to several different ********s? That’s easy, too, and we’ll show you how to do that with one command.

Normally, to copy one file, you would use the cp command, linking to the source file and the destination directory:

Kod:
cp ~/********s/FileToBeCopied.txt ~/TextFiles/

To copy it to two more directories, many people would just run the command two more times, with different destinations:

Kod:
cp ~/********s/FileToBeCopied.txt ~/Dropbox/

Kod:
cp ~/********s/FileToBeCopied.txt /media/lori/MYUSBDRIVE/

642x263x01_entering_cp_command_three_times.png.pagespeed.gp+jp+jw+pj+js+rj+rp+rw+ri+cp+md.ic.PPeFoaMo_F.png


However, we can do the same task with one command:

Kod:
echo dir1 dir2 dir3 | xargs -n 1 cp file1

Here’s how this command works. The echo command normally writes to the screen. However, in this case, we want to feed the output of the echo command as input to the xargs command. To do this, we use the pipe symbol ( | ) which feeds output from one command as input to another. The xargs command will run the cp command three times, each time appending the next directory path piped to it from the echo command on to the end of the cp command. There are three arguments being passed to xargs , but the -n 1 option on the xargs command tells it to only append one of those arguments at a time to the cp command each time it’s run.

So, to stick with our example from earlier, the three separate cp commands above can be combined into one command like this:

Kod:
echo ~/TextFiles/ ~/Dropbox /media/lori/MYUSBDRIVE | xargs -n 1 cp ~/********s/FileToBeCopied.txt

642x253x02_using_single_command.png.pagespeed.gp+jp+jw+pj+js+rj+rp+rw+ri+cp+md.ic.TFNqB3Ep0q.png


Note that if the file being copied exists in any of the destination directories specified, the file in that destination will be replaced automatically. You will not be asked if you want to replace the file. (Normally, when you use the cp command to copy a file to a single ********, you can add the -i option to ask if you want to replace an existing file. However, the -i option is an interactive option (it causes the cp command to ask for input from the user) and you cannot use an interactive option with the cp command when using it in conjunction with xargs .)

One other thing to consider, is that if you are copying a very large file, you might want to add the no-clobber ( -n ) option to the cp command in the single command above. This option automatically prevents a file from being overwritten in a destination if it already exists there. If you’re copying a very large file over a network, it may be slow and you might want to a**** using the resources required to copy and replace the file. The following command adds the -n option, and will not copy the file to any destination listed in the arguments to the echo statement, if the file already exists in that destination.

Kod:
echo ~/TextFiles/ ~/Dropbox /media/lori/MYUSBDRIVE | xargs -n 1 cp -n ~/********s/FileToBeCopied.txt

642x259x03_single_command_with_no_clobber.png.pagespeed.gp+jp+jw+pj+js+rj+rp+rw+ri+cp+md.ic.7GYwgH5hZY.png


Type man echo, man xargs, or man cp on the command line in Linux for more information about any of these commands.



Source:HowToGeek

Note: The censored word is : d o c u m e n t
 
Son düzenleme:

Cave-Man

Kıdemli Üye
21 Nis 2015
3,120
0
Lüksemburg
Can you replace the
Kod:
[IMG]http://www.howtogeek.com/wp-content/uploads/2016/12/650x300x00_lead_image_copying_to_three_********s_w ith_one_command.png.pagespeed.gp+jp+jw+pj+js+rj+rp +rw+ri+cp+md.ic.M5yvsQboiL.png[/IMG]



with this code?
Kod:
[IMG]goo.gl/0x8VUi[/IMG]



Because we can't see the first image.

Why did you shorten the link? It is against the rules dude.
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.