Search and replace string in multiple files (and skip if necessary)

There are two easy ways here (well, more, but I’m only going for two):

  • Perl
    • quick and simple and easy syntax
    • easy as “pie” (get it?)
    • perl -p -i -e 's/PASSWORD/NEWPASSWORD/g' *.php
    • wrap it in a script for safekeeping for the future 🙂
  • sed
    • it was made for strings
    • got to check back for the syntax, but oh so powerful
    • find . -name .phtml -prune -o -exec sed -i 's/PASSWORD/NEWPASSWORD/g' {} \;
    • skip phtml files above, running on files from local directory downwards, replacing PASSWORD with NEWPASSWORD.

Don’t make your password PASSWORD. Or NEWPASSWORD, for that matter.
Ideally, don’t store your password in code, anyway. Use a bootstrap init file – or just store it in your config file; have the Framework pick up the appropriate details for the DB server you’re working on (but that’s another post…)

Leave a Reply

Your email address will not be published. Required fields are marked *