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 <MailList@NerdWorld.org>
 >
> 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 
this.

* 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
	131072

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 
http://www.gnu.org/software/coreutils/faq/coreutils-faq.html#Argument-list-too-long 
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
	2.6.24-19-generic

	$ getconf ARG_MAX
	131072


Shutting up now,
JP

Resources:
* 
http://www.gnu.org/software/coreutils/faq/coreutils-faq.html#Argument-list-too-long
* _bash Cookbook_
----------------------------|:::======|-------------------------------
JP Vossen, CISSP            |:::======|        jp{at}jpsdomain{dot}org
My Account, My Opinions     |=========|      http://www.jpsdomain.org/
----------------------------|=========|-------------------------------
"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         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug