Mac OS X – find files by content text

Question

On my computer I’d like to find all the files that contains a given text. How to do it on a Mac ? Operating system: OS X Yosemite

Solution

There is no visual interface for this feature. Better to open Terminal. Navigate to your folder or stay in the root where you start your search from. Use grep to find the “contenttext” in every file all the way down through the directory tree recursively.

cd /yourpathorroot/
grep -lr "text to find" *

Grep (General Regular Expression) is ready to use complex search strings. For example find all php, html and html files that contain phone number and redirect the result to a file:

cd /yourpathorroot/
grep -irl '[0-9]\{3\}-[0-9]\{4\}' *.php *.htm *.html > result.txt