# Search File Content Recursively



## Ronaldr (Aug 21, 2009)

Hello All,

I'm looking for a particular string or phrase in a wide group of files.

 is there a way to search the content of all files for a particular string/phrase match ?


----------



## jrick (Aug 21, 2009)

Ronaldr said:
			
		

> Hello All,
> 
> I'm looking for a particular string or phrase in a wide group of files.
> 
> is there a way to search the content of all files for a particular string/phrase match ?



Yes. Want you want to use is a tool called grep(1).

I'll leave it up to you to read the manpage, but generally you should be able to do something like:

`% grep -r 'your search here' *`


----------



## DutchDaemon (Aug 21, 2009)

If you expect a lot of hits, use [cmd=]grep -rli[/cmd]. This will *r*ecurse into a directory tree, show only a *l*ist of files that contain the string, and use case-*i*nsensitive matching.


----------



## SirDice (Aug 21, 2009)

Or, if you only want to search in .txt files, combine it with find(1).

`$  find /some/dir -name '*.txt' -exec grep -li 'searchstring' {} \;`


----------



## speed (Sep 15, 2009)

SirDice said:
			
		

> Or, if you only want to search in .txt files, combine it with find(1).
> 
> `$  find /some/dir -name '*.txt' -exec grep -li 'searchstring' {} \;`



You could try the following command to add line-number and color to grep's result.

find /some/dir -name '*.txt' -exec grep --color -n 'searchstring' {} /dev/null \;


----------



## Carpetsmoker (Sep 15, 2009)

Ack:
http://www.freshports.org/textproc/p5-ack/

Better and more advanced than grep -r. And much less typing than find . -[...]


----------



## tankist02 (Sep 15, 2009)

To speed up this command:

 find /some/dir -name '*.txt' -exec grep -li 'searchstring' {} \;

I use xargs:

 find /some/dir -name '*.txt' | xargs grep -li 'searchstring'


----------



## bb (Sep 16, 2009)

Even faster:


```
grep -lr --include "*.txt" 'searchstring' /some/dir
```

Also, be aware that the grep tool uses a pattern for the search string. Make sure to understand the section REGULAR EXPRESSIONS of the grep manual page.

If you want to avoid using patterns all together, use the -F switch like this:


```
grep -Flr --include "*.txt" 'searchstring' /some/dir
```


----------

