Posts Tagged ‘rpl’

How to search all files for a text string:

grep -lr string ./ > results.txt

-l will print only the path and file names of the matches.

Search all files of a certain name for a certain string:

grep -lr string ./ | grep filename

Yes, do specify ./ if you are doing all directories within current.

(source and more info)

Find an replace a string in files:
Searching for a way to do this usually leads me to finding fancy scripts that don’t work right, are scary (create temp files, etc) or aren’t recursive. Just have backups at hand and test the following combination of find and sed before unleashing it, because it works great! You can test it by replacing sed with grep

find ./ -type f -exec sed -i ’s/string1/string2/’ {} \;

Within specific file names/extentions…

find ./ -iname \*.htm\* -exec sed -i 's/\-2005/\-2007/g' {} \;

Don’t forget to use escape \ as seen above. And if your strings contain forward slash / just use a different delimiter such as pipe | or escape it.

You can also install rpl, as shown in the source pdf, for even cleaner line of code for executing your find and replace.

(source and more info) A nice pdf to keep by your side.