Electricmonk

Ferry Boender

Programmer, DevOpper, Open Source enthusiast.

Blog

Linux search and replace

Monday, November 9th, 2009

I always kept a small Python script around for searching and replacing in Linux. Turns out that GNU sed has an inline edit mode which I didn’t know about:

       -i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if extension supplied)

This makes searching and replacing in files as simple as:

find . -name "*.txt" -print0 | xargs -0 sed -i "s/foo/bar/"

This replaces all occurences of “foo” with “bar” in all the .txt files in or below the current directory.

Unfortunately, -i appears to be a GNU extension, so it won’t work on *BSD or Solaris, probably.

The text of all posts on this blog, unless specificly mentioned otherwise, are licensed under this license.