replace text in many files

Change a word or a phrase in every file under a folder that contains it.

  • text
  • files

Steps

  1. grep -rl 'old' .

    first, see which files contain it

  2. grep -rl 'old' . | xargs sed -i 's/old/new/g'

    change it in all of them

  3. find . -name '*.txt' -exec sed -i 's/old/new/g' {} +

    only in the .txt files

  4. git diff

    in a git repository, check what changed

Notes

  • Run it in a git repository or on a copy: a wrong pattern changes many files at once.
  • macOS: sed -i '' in place of sed -i.

Commands used

  • grep
  • sed
  • find
  • xargs

Each has a page in lore: lore grep opens it.