Here's a lazy approach:
grep -rl $oldstring public_html/ | xargs -d'\n' sed -i "s@$oldstring@$newstring@g"
By default, xargs
uses whitespace as the delimiter of arguments coming from the input. So for example if you have two files, a b
and c
, then it will execute the command:
sed -i 's/.../.../' a b c
By telling xargs
explicitly to use newline as the delimiter with -d '\n'
it will correctly handle a b
as a single argument and quote it when running the command:
sed -i 's/.../.../' 'a b' c
I called a lazy approach, because as @Barmar pointed out, this won't work if your files have newline characters in their names. If you need to take care of such cases, then use @Barmar's method with find ... -print0
and xargs -0 ...
PS: I also changed s@"$oldstring"@"$newstring"@g
to "s@$oldstring@$newstring@g"
, which is equivalent, but more readable.