Find (and Replace) Within Files

Get a list of files containing a certain string.

Using 'find' and 'grep' (and 'sed' to take care of filenames with spaces)


Mac OS X 10.4 users can use mdfind, the command line counterpart to Spotlight.


Use the '-live' switch to monitor the results of this search in real time.


grep patterns

PRETTY OUTPUT

ignore binary files
grep -Ilr 'STRINGTOFIND' /PATH/TO/FOLDER | sort

whole word matches
grep -lrw 'STRING' /PATH/TO/FOLDER | sort

DETAILED OUTPUT:

Show the file name, line number, and the line - not pretty, but useful
grep -nHrw 'STRING' /PATH/TO/FOLDER


Find and Replace strings within a batch of files

perl -pi -e 's/find/replace/g' *.txt

Or, to change files in a nested folder structyre:
find . -name '*.txt' | xargs perl -pi -e 's/find/replace/g'

ALWAYS DOUBLE-CHECK FIRST, ESPECIALLY WHEN USING ANY SPECIAL CHARACTERS! YOU COULD EASILY DO YOURSELF A LOT OF DAMAGE!!