Register
It is currently Fri Oct 24, 2014 5:04 pm

Shell script to copy one file to multiple directories


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Sun Mar 29, 2009 4:02 pm   

Joined: Sun Mar 29, 2009 3:48 pm
Posts: 2
I have cpanel set up on a redhat server and I am trying to execute a shell script that will take one file and copy this same file to multiple directories (hosting folders) in the same folder hierarchy. I tried to do this using a wildcard symbol * and instead of doing them all I was testing with all folders that start with an a (hence I used 'a*' in code)

Code:
Code:
#/bin/bash
for file in /home/cccarz/public_html/privacy-policy.php /home/a*/public_html
do
cp privacy-policy.php ${file}
done

When I executed this shell script I got these messages...

Code:
cp: cannot stat 'privacy-policy.php': no such file or directory
cp: cannot stat 'privacy-policy.php': no such file or directory
cp: cannot stat 'privacy-policy.php': no such file or directory
cp: cannot stat 'privacy-policy.php': no such file or directory
cp: cannot stat 'privacy-policy.php': no such file or directory
cp: cannot stat 'privacy-policy.php': no such file or directory
cp: cannot stat 'privacy-policy.php': no such file or directory

.............(it went on 17 lines for the 17 accounts)

What does this mean? Why did I get these messages? Did I put something in the wrong place in my code?


Top
 Profile  
 PostPosted: Mon Mar 30, 2009 1:02 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Seems like privacy-policy.php is not in the location you try to copy from.

Do it like this instead.

Code:
#!/bin/bash
for file in /home/a*/public_html; do
   cp /home/cccarz/public_html/privacy-policy.php ${file}
done


another way would be to do it like this (This is untested thou):
Code:
find /home -type d -iname a* -maxdepth 1 -exec cp /home/cccarz/public_html/privacy-policy.php {}/public_html/ \;


Top
 Profile  
 PostPosted: Mon Mar 30, 2009 8:21 am   

Joined: Sun Mar 29, 2009 3:48 pm
Posts: 2
This code worked great but I beleive what made my shell script work was chowning it to root:root (I also moved it into /home directory). But here is the code that worked...
Code:
#/bin/bash
for file in $(find /home/a* -name public_html -type d)
do
cp privacy-policy.php ${file}
done

I also moved the privacy policy file into /home directory (same as the location of the shell program)
I then just did all the directories after the test...
Code:
#/bin/bash
for file in $(find /home/* -name public_html -type d)
do
cp privacy-policy.php ${file}
done

and it worked beautifully.


Top
 Profile  
 PostPosted: Thu Sep 24, 2009 11:37 am   

Joined: Thu Sep 24, 2009 11:32 am
Posts: 1
for those who come upon this later; I'd just like to point this out:

If you're using find, use the features that come with it to save some cpu cycles:

Code:
find /home/a* -name public_html -type d -exec cp privacy-policy.php {} \;


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP