replace a string recursively in multiple files
In a folder I have lots of files, and the folder also has subdirectories with further files. I want to replace a string in all of them. I want to modify the files in place.
For instance, you want to update a bunch of Python scripts from version 2 to version 3, thus you want to modify the first lines from “
#!/usr/bin/env python2” to “
I found the solution here:
grep -rli 'old-word' * | xargs -i@ sed -i 's/old-word/new-word/g' @
WARNING! Since it modifies the files in place, be very careful! First run just the beginning of the command above:
grep -rli 'old-word' *
Refine it if necessary. If it finds exactly what you need, then proceed.