Wednesday, April 1, 2009

Linux recursive global text replace

Unsharptech has a Linux recursive (sub-directories) global text search and replace command that really works if you are running Linux or administering/webmastering at a Linux host. I'll paste it here as a backup, but I highly recommend their page for its cool formatting.

Notes:
  • there are THREE places to put your text. You have to tell grep what to find and then sed what to find and replace.
  • The grep find string doesn't have to exactly match the sed find string. That's your business.
  • Don't forget to put a backslash "\" (escape code) before any character you think or know is "special". If the command doesn't work for you, check this first and second and third. For example, periods, apostrophes, and quotation marks (double quotes) are "special": s/3\.1416\"/3\.1416\'/


grep -rl 'what_to_find' ./ | xargs sed -i 's/what_to_find/what_to_replace_with/g'

No comments: