BashScripts.org
http://bashscripts.org/forum/

Shell script to copy one file to multiple directories
http://bashscripts.org/forum/viewtopic.php?f=16&t=721
Page 1 of 1

Author:  cebuy [ Sun Mar 29, 2009 4:02 pm ]
Post subject:  Shell script to copy one file to multiple directories

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?

Author:  fredrik.eriksson [ Mon Mar 30, 2009 1:02 am ]
Post subject: 

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/ \;

Author:  cebuy [ Mon Mar 30, 2009 8:21 am ]
Post subject:  Found solution

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.

Author:  munky [ Thu Sep 24, 2009 11:37 am ]
Post subject:  Re: Shell script to copy one file to multiple directories

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 {} \;

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com