I am trying to untar several files at once (I know I can do it differently but I want to make this work because it should work). So I do:
ls -1 *.gz | xargs tar xf
This produces one command from xargs with all files, and fails. -1 is optional - fails the same way without it.
In fact,
ls -1 *.gz | xargs -t echo
echo host1_logs.tar.gz host2_logs.tar.gz host3_logs.tar.gz host5_logs.tar.gz
host1_logs.tar.gz host2_logs.tar.gz host3_logs.tar.gz host5_logs.tar.gz
I tried unsetting IFS, setting it to newline. How do I make xargs on Mac OS X actually work?
Bonus question, in light of linebreak/loop problems I had with other commands before I wonder if Mac terminal is just broken and I should replace all utilities with GNU.
Use the
-n
argument to forcexargs
to run the given command with only a single argument:Otherwise, it tries to use each line from the input as a separate argument to the same invocation of the command.
Note that this will fail if any of the matched file names contain newlines, since
ls
has no way of producing output that distinguishes such names from a sequence of newline-free file names. (That is, there is no option tols
similar to the-print0
argument tofind
, which is commonly used in pipelines likefind ... -print0 | xargs -0
to guard against such file names.)Your question implies that you realize that you could do something like:
which is unlikely to be noticeably slower than any attempt at using
xargs
. In each case the process of spawning multipletar
processes is likely to outweigh any differences in looping inbash
vs the internal loop inxargs
.