Finding, Searching, Replacing & Deleting Strings in Files

To find a text string within files in all sub-directories:

find . -type f | xargs -r0 grep -F ‘dodgystring’

or

find . -type f -exec grep -l “dodgystring” {} \;

To replace a string within files in all sub-directories:

find ./ -type f -exec sed -i ‘s#fromstring#tostring#’ {} \;

From within vi replacing all strings with another:

:%s#fromstring#tostring#g

To remove a large number of files containing a string:

find . -type f -name ‘*.log’ | xargs grep -l “dodgystring” | xargs rm

 

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>