siriusfox Posted September 27, 2007 Share Posted September 27, 2007 Does anyone know of a way, within bash (linux), to within every file in a directory, replace every instance of a string with another. I've got a very poorly written web script that I need to do some modifications to, but the files within said directory statically rely on the name of the directory. So within directory abc lets say, there are a bunch of PHP, config, xml, and some static HTML files. I need a script or command, that will go through all of those files and replace every instance of abc with abc_beta or something of that nature. Any help would be great. -Siriusfox Quote Link to comment Share on other sites More sharing options...
soulbleed Posted September 27, 2007 Share Posted September 27, 2007 find . -name '*.html' -print0 | xargs -0 perl -pi -e 's/oldstring/newstring/g' Quote Link to comment Share on other sites More sharing options...
puredistortion Posted September 28, 2007 Share Posted September 28, 2007 Here is an example all done in bash so you can use on system with no perl. ${string/replace/parttern/} With that pattern is just a standard REGEX I .have included an example that will clear out the 1 for a string 1234. [bash]$ a=1234 [bash]$ echo ${a//[^2-4]/} 234 [bash]$ Quote Link to comment Share on other sites More sharing options...
cooper Posted September 28, 2007 Share Posted September 28, 2007 find . -name '*.html' -exec sed -i 's/oldstring/newstring/g' {} ; Note that the slashes before the accolades and the semicolon, as well as the space before the escaped semicolon are required. FYI, that bit get replaced by every matching file that find is able to find. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.