Tuesday, January 31, 2012

grep searchs a string in filename wildcards recursively

To search all files under /dir for "blah", run the following:

# grep -r blah /dir

You can not use wildards directly in this manner, for example, the following will not search all text files:

# grep -r blah *.txt

This doesn't work because the wildcard is expanded by the shell before grep is called. Instead, search the current directory (or whichever one you want) and pass the --include option:

# grep -r blah . --include "*.txt"

Reference:
http://mindspill.net/computing/linux-notes/recursive-grep-and-filename-wildcards/

No comments: