[CLUE-Tech] xargs

marcus hall marcus at tuells.org
Mon Jan 6 07:36:04 MST 2003


On Sun, Jan 05, 2003 at 07:54:21PM -0700, Kevin Fenzi wrote:
> marcus> containing shell meta-characters, and shell scripts are
> marcus> supposed to be able to be written carefully enough to not mind
> marcus> it, but this behavior of xargs breaks the "find ... | xargs"
> marcus> idiom and there's no way that I see to get it to work if there
> marcus> are any files that contain meta-characters.  That's why I
> marcus> contend that it is wrong behavior.
> 
> It's worth noting the -print0 argument for find and the xargs -0
> argument. That makes them use a null to terminate filenames instead of
> white space... 
> 
> so: 
> 
> find /whatever -print0 | xargs -0 ls 
> will work on even files with strange embeded characters. 

Thank you..

I woke up this morning and realized that my rant about xargs last night
was somewhat unfounded, since xargs' man page says that it breaks apart
whitespace delimited strings and of course white space is perfectly
acceptable (although somewhat inconvenient at times) to occur in a file
name.  I was ready to re-look at the man pages for xargs and find to
see if there was something to make the common idiom work.  I did recall
the -0 argument to xargs, but didn't recall the -print0 argument to find.
Thanks Kevin for pointing that out.  I had started to panic this morning
and think that there might not be a way to make sure that shell scripts
*could* get it right...

The problem with using --exec in find is that if there are a very large
number of files, then you are executing the command a very large number
of times.  If there is any substantial setup overhead before the per-file
operation of the target command, the "find ... --exec command \{} \;"
type of script will incur the setup overhead for each and every file, and
thus will run much slower than "find ... | xargs command" which will
spread the overhead over several hundred files.  Thanks to Kevin, I
will have to go back through my scripts and correct them so that they
work on odd but legal files.

Wow, I did just re-read the xargs man page, and it *does* say that the
blanks may be protected by double or single quotes or a backslash.  So
I must totally retract my rant from last night...  I guess xargs *is*
working the way it is documented to work...  And thanks to Kevin, I now
know how to make it work even better than how I thought it was supposed
to work...

The immortal words "Never Mind.." apply right about now...

marcus hall
marcus at tuells.org



More information about the clue-tech mailing list