find command

Bob Drzyzgula redhat at drzyzgula.org
Tue Mar 23 12:44:49 UTC 2004


On Tue, Mar 23, 2004 at 02:43:56AM -0800, IAK Tanoli wrote:
> 
> find ./ -name <filename> -depth -print | grep "text"

That will find files with *names* containing the
string "text". If you want to search the *contents*
of the file, you need to use the -exec construct,
e.g. 

  find . -type f -depth -exec grep -H "text" \{\} \;

The -H argument to grep will force it to print
out the matching filename in question.

Alternatively, if one just wants to recursively
search for a string in all files under a directory,
it can be easier to use the -r switch on (GNU) grep.
The following command will have pretty much the same
effect as the previous find command:

  grep -ri "text" .

OTOH, the find command gives you much more control
over which files will be searched, so that if for
example you wanted to search only files with names
ending in ".txt", 

  find . -name \*.txt -type f -depth -exec grep -H "text" \{\} \;

should do the trick.

--Bob





More information about the redhat-list mailing list