[X-Unix] question about 'find'

Stewart C. Russell scruss at scruss.com
Thu Dec 1 10:33:46 PST 2005


David Ledger wrote:
>
>> You do have to be careful with xargs if you're processing a *lot* of
>> arguments. BSD xargs has a default argument limit of 5000 (hidden in the
>> '-n' para of the manual), so if you're calling xargs through sort (say),
>> the results may not be what you expect.
> 
> That's not what it means.

It might not be what it means, but it's what it does.

On OS X:

$ perl -le 'for (1..5000) {print $_;}' | xargs echo | wc -l
        1
$ perl -le 'for (1..5001) {print $_;}' | xargs echo | wc -l
        2

So in effect, there's a default '-n 5000' in the xargs invocation, for 
in the second case 'echo' is being called twice. It's different on other 
unices, f'rinstance with Gnu xargs on Cygwin:

$ perl -le 'for (1..5954) {print $_;}' | xargs echo | wc -l
1
$ perl -le 'for (1..5955) {print $_;}' | xargs echo | wc -l
2

Gnu xargs has a default maximum command line length of 128KB, so in this 
case, the rollover to two command invocations comes after more input 
than does OS X.

It's not safe to assume that xargs will always make just one invocation 
of its command argument. It's definitely a trap for the unwary.

> As a pedantic sidenote, the arguments to xargs are what you type as 
> arguments.

Is this the five minute argument, or the full half hour? ;-)

> On OS X, xargs thinks all commands can cope 
> with 5000 arguments unless you specify otherwise with '-n <number>'.

Which is what I said.

> I define a shell function which I call fnf (FileName Fix):
>     fnf() { sed "s/\([      '\"]\)/\\\\\1/g"; }

Handy! Thanks.

> If you mean piping the stream into a while loop and invoking the 
> sub-command once for each item, that's even less efficient than 
> '-exec'.

But I'm generally running scripts that:
* filter one input file into one output file, so a loop makes sense, and
* take a trivial amount of time to run compared to the invocation of 
each filter.

If you're worried about efficiency over expediency, you shouldn't be 
using shell.

cheers,
  Stewart


More information about the X-Unix mailing list