How To Copy Single File To Multiple Directories In Linux (Redhat / Centos / Ubuntu / Debian)

How To Copy Single File To Multiple Directories In Linux (Redhat / Centos / Ubuntu / Debian)

Hi Everyone……

This is very common post but very useful when you want copy a file to multiple directories. In my scenario I have single file (test.ini) and I want to paste it into multiple directories (almost 1 Lakh directories). Now due to huge number of destination directories you cannot type all directories name & also you cannot also do it with simple cp command.

Here is the solution:

I assume we have a file name (test.ini) and paste it into multiple directories which are located under ( /usr/local/).

 

root@test:~# cd /usr/local/

root@test:/usr/local#

root@test:/usr/local# echo [a-z]* | xargs -n 1 cp -p /root/test.ini
root@test:/usr/local#

root@test:/usr/local# echo [A-Z]* | xargs -n 1 cp -p /root/test.ini

Here:–

echo [a-z]*  —–> With the help of wild card we can get all directories name which name are start from a character To z character. This output will become the input of xargs command

xargs ——->  Build and execute command lines from standard input

cp ——> copy file test.in and paste  it into the  directories which name  you got from echo [a-z]* output.

 

Now your test.in file copy to multiple directories.

 

Enjoy {#moods_dlg.WellDone}

Leave a Reply

Time limit is exhausted. Please reload the CAPTCHA.