Kevin Shackleton <[EMAIL PROTECTED]> writes:
>> find *.zip -print0 | xargs -0 unzip
>> > ) is not doing it for me.
>
> Thanks for the three views of solving this problem.
>
> I had always thought that xargs was invoked once per find, so Amos'
> solution alerted me to the --max-args option.
*nod* For something where you want to invoke one process per argument
then 'find ... | while read file; do whatever to "$file"; done' is
traditional.
The main purpose of xargs, in fact, is to ensure that you run the
minimum number of processes depending on the maximum argument list size
on your platform.
> However, it still did not work - unzip treated each word in the
> filename as a zip file which of course failed.
Actually, no: xargs always passes the arguments it reads to the process
unmodified, as a single argument: there is no shell expansion.
So, if xargs reads a line with a space (when -0 is engaged), that will
be passed as *one* argument to whatever tool you specify:
] printf 'one two\0three\0' | strace -ff -e execve xargs -0 -n1 /bin/true
execve("/usr/bin/xargs", ["xargs", "-0", "-n1", "/bin/true"], [/* 51 vars */])
= 0
Process 29739 attached
Process 29738 suspended
[pid 29739] execve("/bin/true", ["/bin/true", "one two"], [/* 51 vars */]) = 0
Process 29738 resumed
Process 29739 detached
--- SIGCHLD (Child exited) @ 0 (0) ---
Process 29740 attached (waiting for parent)
Process 29740 resumed (parent 29738 ready)
[pid 29740] execve("/bin/true", ["/bin/true", "three"], [/* 51 vars */]) = 0
Process 29738 suspended
Process 29738 resumed
Process 29740 detached
--- SIGCHLD (Child exited) @ 0 (0) ---
Process 29738 detached
See the single argument each time, including the one with the spaces?
> This solution needs something more to quote the filename containing
> spaces.
Your problem was that unzip interprets the first argument as the
zipfile, and the second and subsequent arguments as files to extract
from it, not because of the spaces as such.
It would file for this situation, also:
] ls
1.zip 2.zip 3.zip
] echo *.zip | xargs unzip
# ... doesn't work
> Patrick's solution of adding an explicit -name option and quoting the
> ambiguous filename didn't work as such - it needed Amos' --max-args,
> then it worked.
This is because you ended up running unzip with only one argument, not
multiple, in that situation.
> Daniel's solution was a tidy alternative that did work.
I just cheated; it wasn't actually different from Amos' solution, except
that I didn't bother with either find or xargs -- since I happen to know
that unzip (and unrar, FWIW) don't work with more than one "file to
extract from" argument at a time.
Regards,
Daniel
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html