Find replace recursively over directory

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Find replace recursively over directory

Post 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'"
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

find . -name '*.php' -exec mv {} {}.old; sed "s/OB::loadClass('/OF::loadClass('OB_/g" {}.old > {} \;

Untested
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Ah thank you. That first post had me scratching my head.
Post Reply