Had a client ask to copy a file to multiple public_html directories, came across this little bash script which worked beautifully:
touch copy.sh
vim copy.sh
add this to file, modifying the name of the file to be copied (in this case, test.php)
#/bin/bash
for file in $(find /home/* -name public_html -type d)
do
cp test.php ${file}
done
once the file is created, make it executable
chmod +x copy.sh
Now, just run it as you would any other script
sh copy.sh
There is also a method provided by cyberciti.biz which works very well also.
Someone also mentioned “what if you want to remove those same files?”
The safest way would be to first run a backup of the account and then use a bash command like so;
find /home/*/public_html/ -type f -name "test.php" -exec rm -i {} \;
This will locate the test.php file in all of the public_html directories and prompt you to allow or deny the delete
[root@host.domain.com] home >> find /home/*/public_html/ -type f -name "test.php" -exec rm -i {} \;
rm: remove regular file `/home/user1/public_html/test.php'? y
rm: remove regular file `/home/user2/public_html/test.php'? y
rm: remove regular file `/home/user2/public_html/test/dir1/test.php'? n
rm: remove regular file `/home/user2/public_html/test/dir2/test.php'? n
rm: remove regular file `/home/user3/public_html/test.php'? y
rm: remove regular file `/home/user4/public_html/test.php'? y
rm: remove regular file `/home/user5/public_html/test.php'? y
rm: remove regular file `/home/user6/public_html/test.php'? y
rm: remove regular file `/home/user7/public_html/test.php'? y
rm: remove regular file `/home/user8/public_html/test.php'? y
rm: remove regular file `/home/user9/public_html/test.php'? y
rm: remove regular file `/home/user10/public_html/test.php'? y
This should work for small amounts of accounts searched. If you have a large amount of files with the same name, you may be able to use the -mtime tag to limit the search even further.