Wednesday, August 5, 2009

To find a word in a file

SkyHi @ Wednesday, August 05, 2009
[root@home ]# find . -type f -exec grep -iH 'Vancouver' {} \;

grep command: Recursively Search All Files For A String

cd /path/to/dir
grep -r "word" .

grep -r "string" .

Ignore case distinctions:
grep -ri "word" .

To display print only the filenames with GNU grep, enter:
grep -r -l "foo" .

You can also specify directory name:
grep -r -l "foo" /path/to/dir/*.c

find command: Recursively Search All Files For A String

find command is recommend because of speed and ability to deal with filenames that contain spaces.
cd /path/to/dir
find . -type f -exec grep -l "word" {} +
find . -type f -exec grep -l "seting" {} +
find . -type f -exec grep -l "foo" {} +


Older UNIX version should use xargs to speed up things:
find /path/to/dir -type f | xargs grep -l "foo"

It is good idea to pass -print0 option to find command that it can deal with filenames that contain spaces or other metacharacters:
find /path/to/dir -type f -print0 | xargs -0 grep -l "foo"

REFERENCES
http://www.cyberciti.biz/faq/howto-recursively-search-all-files-for-words/