« What year is it? | Main | Ruby on Rails - shaky »

June 13, 2006

Linux - searching inside files

If you need to find a list of files that contain a certain string on a Linux box the following is useful:

find . -name '' -exec grep 'String To Search For' {} \;

you can also do this:

find . -name '*.filext' -exec grep 'string here' {} \;

which would limit the results to showing only those files that contain the search string and also end in '.filext'

Update:

Also the following works on other versions of Linux shells (bash?):

find . -exec grep "woteva" '{}' \; -print

and case insensitive just chuck in the regexp 'i' - I guess the other regexp directives would do their thing also:

find . -exec grep -i "woteva" '{}' \; -print

Posted by dottie at June 13, 2006 4:01 PM

Comments

GNU grep also has a -r flag which allows for recursive searches, so you often don't need the find command. I typically use this:


alias grep='grep --recursive --color=auto'

This also provides color highlighting of the matches.

You might also be interested in this post on my blog.

Posted by: Marc at June 13, 2006 6:08 PM

That's a great tip, thanks Marc :)

Posted by: Mark Lennox at June 13, 2006 11:17 PM

Post a comment




Remember Me?

(you may use HTML tags for style)