JP Vossen on 16 Oct 2008 12:21:22 -0700

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] CLI for "find" text string in an unknown file

> Date: Wed, 15 Oct 2008 19:10:00 -0400
> From: Casey Bralla <>
> I'm looking for a command line tool that will search through text files and 
> find the ones with a specific string.   So far, Google has not helped me.   
> Anybody know how to do this?

Wow, what a firestorm...  So of course I have to add fuel... :-)

+10 for: grep -rl <string> /path/to/*/directory
+5  for: find <directory> -print0 | xargs -0 grep -l string
-10 for: find <directory> -exec grep -l <string> {} \;

Some notes:

* find -exec is not only very ugly syntax wise (as various folks pointed 
out), it's extremely inefficient (as Jason Stelzer pointed out).  Avoid 

* On Linux, or anyplace else with GNU tools, it's best to get into the 
habit of 'find -print0 | xargs -0' as that will handle spaces and other 
odd things in names.

* You can use shell wildcards in the '/path/to/*/directory' part, which 
can be very handy, e.g., /reports/200810*/.

* BUT, as other folks have mentioned, you can also run into "argument 
list too long" errors, in which case you can use a for loop or a (wait 
for it) 'find ... -print0 | xargs -0 ...' command.  See recipe 15.13 on 
page 343 of the _bash Cookbook_.  :-)

By the way, if you are wondering what the limit is, 'getconf ARG_MAX' 
will tell you.  My Linux hosts give me:
	$ getconf ARG_MAX

When I last tested this (circa 2005-2006), FreeBSD was the worst at 
65536 and HP-UX was the best at 2048000.  But...  See the very handy 
and also note that it says Linux kernel 2.6.23+ removes the limit.  But 
I haven't noticed that yet on my Hardy box:
	$ uname -r

	$ getconf ARG_MAX

Shutting up now,

* _bash Cookbook_
JP Vossen, CISSP            |:::======|        jp{at}jpsdomain{dot}org
My Account, My Opinions     |=========|
"Microsoft Tax" = the additional hardware & yearly fees for the add-on
software required to protect Windows from its own poorly designed and
implemented self, while the overhead incidentally flattens Moore's Law.
Philadelphia Linux Users Group         --
Announcements -
General Discussion  --