I need a way to target all sub directories under public_html which ends with .com or other TLDs like .biz or .co.il I might list, in a heredocument.

This my hereoducmnet inside an pushd-popd - if-fi statement nested inside a for loop:

for dir in "$HOME"/public_html/*/; do
    if pushd "$dir"; then
        cat <<-"EOF" >> "$HOME"/public_html/ALL_DOT_COM|ALL_DOT_BIZ/.htaccess



done 2>myErrors.txt

Is there a way to combine regex to achieve that, directly inside the heredocument redirect/append line (where ALL_DOT_COM|ALL_DOT_BIZ is)?

  • @DopeGhoti question edited: The desired output is the first empty line, the apache_directive set_1, the second empty line, the apache_directive_set_2, and the third empty line. – user9303970 Jun 12 '18 at 18:33

I assume you want to append to .htaccess in all directories like $HOME/public_html/foo.com/, $HOME/public_html/bar.biz/ etc.

Your for loop already manages processing all directories, the question is then just to adjust the glob to just match the required endings:

for dir in "$HOME"/public_html/*.{com,biz,co.il}/; do
    cat <<-"EOF" >> "$dir"/.htaccess
        Apache directives...
done 2>myErrors.txt

There doesn't seem to be any need for the cd, since dir already contains the full path of the directory.

The above will of course only match the listed top-level domains. (I'm not sure if this is what you want, since you wrote "or other TLDs".) If you don't care which TLD there is on the directory name, just use .../public_html/*/, or .../public_html/*.*/, though that still assumes there's nothing else in there. (But then, listing all the TLDs is a bit awkward, and they might come up with more...)

| improve this answer | |
  • Listing as you show is best, I actually preferred that. Thanks. – user9303970 Jun 12 '18 at 20:03

Using tee -a to append to a number of files, and a brace expansion.

I also don't think that the loop is actually needed, as tee does the work of writing into multiple files at once.

cat <<-"EOF" | tee -a "$HOME"/public_html/*.{com,biz,co.il}/.htaccess

The string


will be expanded into

/home/something/public_html/*.com/.htaccess /home/something/public_html/*.biz/.htaccess /home/something/public_html/*.co.il/.htaccess

and then the globs will do their work matching against the appropriate subdirectories.

If there is a risk that a glob might not match anything, you should probably also set the nullglob shell option in bash:

shopt -s nullglob

With this option set, a glob pattern that does not match any existing filename will be expanded to an empty string.

If the pattern "$HOME"/public_html/*.{com,biz,co.il}/.htaccess expands to a string that is far too long, you may have issues running tee. In this case you do need the loop:

for dir in "$HOME"/public_html/*.{com,biz,co.il}/
    cat <<-"EOF" >>"$dir/.htaccess"

There's still no need to cd into the directories though.

| improve this answer | |

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.