grep -rl "Chunky" . | LC_ALL=C xargs sed -i ".bak" "s/Chunky/Bacon/"
Every so often we need to mass edit some directories. That should be the job of sed. Yet sed doesn't find files, so you need another utility. There you have a choice of tools. For this case I decided to use grep. Although I use ack for searching patterns, I thought that grep would be the right tool here.
Now the explanation of the example.
| Magical bit | Explanation |
|---|---|
| grep | Our second favorite tool for finding patterns in files. Our favorite is ack, but grep is more appropriate here. |
| -rl | Our grep options. r = recursive search. l = output the name of the files |
| "Chunky" | Our pattern |
| . | The direction we want to search. In this case, start from the current directory |
| the pipe | The magic unix character that lets us pass output into another command |
| LC_ALL=C | The esoteric incantation to have sed behave in MacOS. It has to do with encoding |
| xargs | Unix utility to transform standard input into command arguments |
| sed | Our batch editor |
| -i | The option to edit in place |
| ".bak" | The extension that we want to give to the backup file that sed -i will create |
| "s/Chunky/Bacon/" | The sed command we want to execute. In this case substitute Chunky with Bacon |
You can clean up the backup files with
grep -rl "Chunky" . | xarg rm -f