Find files containing a text under OSX or Linux

There are two methods I mainly use to find a text in multiple text files i.e. my project directory. If one is just generally looking for any file that may contain the text snippet I use grep:


grep -r "some_method_name" .

This will search all files recursively in your current path. If you only want to search in a sub directory from your current path just replace the dot with the path to the sub folder e.g.: grep -r "some_text" my/log/files

If I know or only want to see it for a certain type of file I’ll use find and xargs e.g. like this:


find . -name "*.erb" | xargs grep "some_method_name"

Again if you only want to search in a given sub directory just replace the dot with the path to the sub folder.

I found these two little bash lines real time savers!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: