[NTLUG:Discuss] find, xargs, grep (was: Renaming a Linux host)

Robert Citek rwcitek at alum.calberkeley.org
Fri Jul 29 14:56:23 CDT 2005


On Jul 28, 2005, at 8:01 PM, Robert Thompson wrote:
> > find /path -type f | xargs grep "foo"
>
> I've had that command puke and die because the arg list to grep was  
> too long.

I don't doubt that a command puked and died, but are you sure it was  
because of using xargs?  Are you sure you are not thinking of command  
expansion?  For example:

   grep "foo" $(find /path -type f )

The whole point of using xargs is so that the argument list doesn't  
get too long.

Can you give a bit more details on what exactly you did when your  
computer balked?  A specific example would be ideal.

> A better version is:
>
> find /path -type f -exec grep "foo" {} \;

That depends on what you mean by better.  In your example a new grep  
process will spawn and die for each file found.  100,000 files means  
100,000 processes.  Using xargs, a new process is spawned for a group  
of files.  On a recent test I did I got groups of roughly 500 files  
per command.  100,000 files means 200 processes, which is a lot  
faster and a lot less resource intensive.

Regards,
- Robert
http://www.cwelug.org/downloads
Help others get OpenSource software.  Distribute FLOSS
for Windows, Linux, *BSD, and MacOS X with BitTorrent





More information about the Discuss mailing list