Search File Content Recursively

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 ?
 
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' *
 
If you expect a lot of hits, use [cmd=]grep -rli[/cmd]. This will recurse into a directory tree, show only a list of files that contain the string, and use case-insensitive matching.
 
Or, if you only want to search in .txt files, combine it with find(1).

$ find /some/dir -name '*.txt' -exec grep -li 'searchstring' {} \;
 
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 \;
 
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'
 
Even faster:

Code:
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:

Code:
grep -Flr --include "*.txt" 'searchstring' /some/dir
 
Back
Top