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

 

Post comment