Skip to Content.
Sympa Menu

sm-discuss - Re: [SM-Discuss] Using xargs

sm-discuss AT lists.ibiblio.org

Subject: Public SourceMage Discussion List

List archive

Chronological Thread  
  • From: Tony Smith <tony AT smee.org>
  • To: sm-discuss AT lists.ibiblio.org
  • Subject: Re: [SM-Discuss] Using xargs
  • Date: Thu, 15 Apr 2004 18:55:35 +0100

No, xargs is almost ALWAYS better. Read-on and you'll see why!

Consider the common case of grepping all files in a tree. The solution most
people use is:

find . -type f -exec grep 'regexp' /dev/null "{}" ;

but this is hideously inefficient. A separate grep is fork()'d and exec()'d
for every single file. Eek!!! Note also the quotes around the {} to cope with
files with spaces in them.

By contrast:

find . -type f -print0 | xargs -0 grep 'regexp'

(a) looks easier on the eye
(b) is WAAAAYY more efficient!

A grep is forked/execed for every 32 worth of argument (and environment)
space.

If there's only one file found, then it's only marginally less efficient than
the first example - you have the overhead of the xargs process.

With two files found, it's (likely) to be equally efficient.

With more than two files, it's going to get progressively more and more
efficient when compared to the common, clunky approach.

With 'find -print0', 'xargs -0' also handles files with whitespace...

xargs is truly your friend.

On Wednesday 14 April 2004 23:56, Paul wrote:
> It's only necessary when you have the possibility of a lot of
> parameters. My guess is 32k. Should you desire to know the exact number,
> here's a one liner for you:
> ( f() { [[ $1 -eq $# ]] && echo "$1==$#"; } ; let i=2; arg="1"; while :
> ; do arg="$i ${arg}"; f $arg || break; let i++; done )
> That should stop when the number of args is wrong. We may use xargs in a
> place or two now, where it makes things easier to understand.
>
> On Wed, 2004-04-14 at 17:42, Eric Sandall wrote:
> > I just saw a brief article[0] on linux.com about xargs and am wondering
> > if that might help our Sorcery team (though from what I remember at least
> > some of them already know about it ;)).
> >
> > [0] http://www.linux.com/article.pl?sid=04/04/13/211209




Archive powered by MHonArc 2.6.24.

Top of Page