Search File Content Recursively

General questions about the FreeBSD operating system. Ask here if your question does not fit elsewhere.

Search File Content Recursively

Postby Ronaldr » 21 Aug 2009, 00:05

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
Junior Member
 
Posts: 37
Joined: 09 Jun 2009, 19:19

Postby jrick » 21 Aug 2009, 00:11

Ronaldr wrote: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 [man=1]grep[/man].

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

[cmd=%]grep -r 'your search here' *[/cmd]
"Gentlemen! You can't fight in here! This is the war room!"
--President Merkin Muffley, Dr. Strangelove
jrick
Junior Member
 
Posts: 84
Joined: 17 Nov 2008, 03:59
Location: Michigan

Postby DutchDaemon » 21 Aug 2009, 09:37

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.
User avatar
DutchDaemon
Old Fart
 
Posts: 10466
Joined: 16 Nov 2008, 20:17
Location: The Netherlands

Postby SirDice » 21 Aug 2009, 09:40

Or, if you only want to search in .txt files, combine it with [man=1]find[/man].

[cmd=$] find /some/dir -name '*.txt' -exec grep -li 'searchstring' {} \;[/cmd]
Senior UNIX Engineer at Unix Support Nederland
Experience is something you don't get until just after you need it.
User avatar
SirDice
Old Fart
 
Posts: 16185
Joined: 17 Nov 2008, 16:50
Location: Rotterdam, Netherlands

Postby speed » 15 Sep 2009, 07:45

SirDice wrote:Or, if you only want to search in .txt files, combine it with [man=1]find[/man].

[cmd=$] find /some/dir -name '*.txt' -exec grep -li 'searchstring' {} \;[/cmd]


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 \;
speed
Junior Member
 
Posts: 8
Joined: 20 Nov 2008, 03:53

Postby Carpetsmoker » 15 Sep 2009, 08:14

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

Better and more advanced than grep -r. And much less typing than find . -[...]
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
User avatar
Carpetsmoker
Member
 
Posts: 905
Joined: 16 Nov 2008, 19:07
Location: Eindhoven, Netherlands

Postby tankist02 » 15 Sep 2009, 20:21

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'
tankist02
Junior Member
 
Posts: 90
Joined: 17 Nov 2008, 22:48

Postby bb » 16 Sep 2009, 06:50

Even faster:

Code: Select all
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: Select all
grep -Flr --include "*.txt" 'searchstring' /some/dir
bb
Junior Member
 
Posts: 42
Joined: 26 Jun 2009, 22:58
Location: Vienna, Austria


Return to General

Who is online

Users browsing this forum: No registered users and 0 guests