Page 1 of 1

Find replace recursively over directory

Posted: Tue Dec 12, 2006 2:52 pm
by Ollie Saunders
I need to make a lot of replacements to a directory of PHP files.
In windows you can do this:

Code: Select all

for /r %file in (*.php) do
but there doesn't seem to be a bash equivalent.

This is the replacement code I'm going to use:

Code: Select all

mv $file $file.old
sed "s/OB::loadClass('/OF::loadClass('OB_/g" $file.old > $file
rm $file.old
For now I've been doing this:

Code: Select all

files="list
of
files
i had to compile myself"
for file in $files do
    echo $file
done
but I get "syntax error near unexpected token `echo'"

Posted: Tue Dec 12, 2006 3:46 pm
by Chris Corbyn
find . -name '*.php' -exec mv {} {}.old; sed "s/OB::loadClass('/OF::loadClass('OB_/g" {}.old > {} \;

Untested

Posted: Tue Dec 12, 2006 4:06 pm
by Chris Corbyn
Tested:

Code: Select all

for x in $(find . -name '*.php')                    
do
     mv $x $x.old
     sed "s/OB::loadClass('/OF::loadClass('OB_/g" $x.old > $x
done
By the way... the indents are required ;)

Posted: Tue Dec 12, 2006 4:10 pm
by Ollie Saunders
Ah thank you. That first post had me scratching my head.