[Cialug] find and replace strig in all files within a folder

Nicolai nicolai-cialug at chocolatine.org
Fri Jun 8 11:48:18 CDT 2012


On Thu, Jun 07, 2012 at 06:35:21PM -0600, afan at afan.net wrote:
> hi,
> what would be the easiest but most simplest way to find and replace string
> "foo" with "bar" in all files within a folder (and all sub-folders)?
> 
> I found this
> $find ./ -name "*.php" -exec sed -i "s/foo/bar/g" {} \;
> and it works. But when I replace foo/bar with $_SESSION['status'] /
> $_SESSION['value'] it doesn't work

It's more informative to copy and paste output, rather than say it
"doesn't work".

Try this:

sed 's/$_SESSION\['"'"'status'"'"'\] /$_SESSION\['"'"'value'"'"'\] /g' blah.php

If that works on a single example file, you can plug the same logic into
your find command.  Tested and works on OpenBSD/ksh and Linux/bash.

Nicolai


More information about the Cialug mailing list