bergman on 16 Oct 2008 08:54:43 -0700


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

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



In the message dated: Thu, 16 Oct 2008 11:36:03 EDT,
The pithy ruminations from "Jason Stelzer" on 
<Re: [PLUG] CLI for "find" text string in an unknown file> were:
=> On Wed, Oct 15, 2008 at 7:14 PM, Matt Mossholder <matt@mossholder.com> wrote:
=> > Try this:
=> >
=> >
=> > find <directory> -exec grep -l <string> {} \;
=> >
=> 
=> Couple fine points on this.

Since we're getting pedantic...

=> 
=> The grep on most linux systems can recurse directory trees so using
=> find isn't needed.
=> 
=> Using -exec with find is very inefficient. It forks off and execs the
=> command you specify for each file you find. For grepping this is over
=> kill. You would be better off building up a list of files and
=> executing grep once over all of them. There are situations where you
=> do actually need to use -exec, but those situations are fairly
=> uncommon.
=> 
=> grep -l <string> ` find <directory> `

That's likely to exceed the shell's limit on the length of the command line or 
the number of arguments, or similar limits within grep*. That will also break 
if you have directories or filenames that contain embedded whitespace.

A more reliable way of doing this is (assuming GNU fileutils):
	
	find <directory> -print0 | xargs -0 grep -l string

	
=> 
=> Or, just grep -lr <directory>

Yep. Modulo the issues with embedded whitespace.

Mark


	* I haven't looked at the code for grep in many years, and GNU utils 
		tend to be good about eliminating arbitrary limits or making
		them huge, but this may still be an issue.
=> 
=> 
=> 
=> -- 
=> J.
=> ___________________________________________________________________________
=> 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
=> 



___________________________________________________________________________
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