I'm trying to write a script that will descend into each subdirectory in /home and find the public_html folder within.

Then it will check if an .htaccess already exists, if it does write that to a file, if it doesn't exist place the .htaccess file in public_html

Right now I have

for d in */; do
cd "${D}"
cd public_hmtl
if [-e $FILE ]; then
echo "Htaccess exists for "${D}" >> /test/error.txt
cp /htaccess ./

This is obviously not right as it doesn't even run. I haven't had much experience with bash and it's been awhile since I've needed to use it.



for dir in /home/*/public_html/; do
    if [ -e "$dir/.htaccess" ]; then
        print 'htaccess exists in "%s"\n' >&2
        cp "$htaccess" "$dir"

This iterates over all public_html directories in each directory under /home and checks to see if there's already a .htaccess file in there. If there is, a diagnostic message is produced, otherwise a standard .htaccess is copied from somewhere.

You would run this with

$ ./script 2>error.log

The issue with your code is the cd. It changes directory down to one of the public_html directories (if run from within /home), but never comes back up again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.