You've chosen an excellent set of tools for the job!
Generating a list of files you want to edit is very simple - I'm here assuming you're using a shell which will expand the output of matching filenames, such as bash, or dash:
echo /home/*/site/assets/.htaccess > list.txt
However, if *
can mean more than 1 level of directories, I would go with this:
find -name '.htaccess' /home | grep '/site/assets/sym' > list.txt
Your shell will expand the expression into a list, and redirect the output to list.txt. One file for each line.
You can also search for files named .htaccess with find - to see if there's anyone that you've missed, like this:
find -name '.htaccess' /home
Adding line breaks in replace text - this depends a bit on your editor. Line breaks are usually represented as \n
What you want to do, can probably be achieved with a small shell-script - when for variable in expression; do .... done
is used, it will execute the lines in between do/done once for each word in expression. NOTE: This can backfire if the paths contain spaces.
for file in /home/*/site/assets/.htaccess; do
sed 's/RewriteRule \^(.\*)\$ \/site\/assets\/sym\/\$1 \[L,NS\]/RewriteCond %{REQUEST_URI} !^\/site\/assets\/sym\nRewriteRule \^(.\*)\$ \/site\/assets\/sym\/\$1 \[L,NS\]/g' $file > $file.new
mv $file $file.old
mv $file.new $file
done
(This will leave a file named .htaccess.old in its spot.)
Rewritten for alternate list-gathering-approach:
for file in $(find -name '.htaccess' /home | grep '/site/assets/sym'); do
sed 's/RewriteRule \^(.\*)\$ \/site\/assets\/sym\/\$1 \[L,NS\]/RewriteCond %{REQUEST_URI} !^\/site\/assets\/sym\nRewriteRule \^(.\*)\$ \/site\/assets\/sym\/\$1 \[L,NS\]/g' $file > $file.new
mv $file $file.old
mv $file.new $file
done
Or, in one line, without taking a backup:
for file in /home/*/site/assets/.htaccess; do sed -i 's/RewriteRule \^(.\*)\$ \/site\/assets\/sym\/\$1 \[L,NS\]/RewriteCond %{REQUEST_URI} !^\/site\/assets\/sym\nRewriteRule \^(.\*)\$ \/site\/assets\/sym\/\$1 \[L,NS\]/g' $file; done