jump to navigation

Find and replace text in files October 22, 2007

Posted by maxmil in : bash , trackback

Taken from here

To replace all occurances of a string:

find /your/home/dir -name "*.txt" | xargs perl -pi -e 's/stringtoreplace/replacementstring/g'

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;

Comments»

no comments yet - be the first?