Terminal: Search and Replace text in all files in a folder and its subfolders
I just stumbled across a neat idea using perl to replace the content in files
To replace all occurances of a string:
find ./ -name “*.txt” | xargs perl -pi -e ‘s/stringtoreplace/replacementstring/g’
for every of these examples you can also use sed
find ./ -name “*.txt” | xargs sed -i ‘s/stringtoreplace/replacementstring/’
or rpl
rpl stringtoreplace “replacementstring” ./
or numeros other solutions …
To replace the first occurance:
find /your/home/dir -name “*.txt” | xargs perl -pi -e ‘s/stringtoreplace/replacementstring/’
To replace all files in a folder:
for arg in `ls -C1`; do perl -pi -e ‘s/stringtoreplace/replacementstring/g’; done;
you can do more cool tricks using the for shell command as demonstrated above. you can add more specific searches. However, you might be better off just writing a shell script. Here is an example of the first find:
for arg in `find /your/home/dir -name “*.txt”` ; do perl -pi -e ‘s/string/replacement/g’ $arg; done;