Use the -n
argument to force xargs
to run the given command with only a single argument:
ls -1 *.gz | xargs -n 1 echo
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 to ls
similar to the -print0
argument to find
, which is commonly used in pipelines like find ... -print0 | xargs -0
to guard against such file names.)
Your question implies that you realize that you could do something like:
for f in *.gz; do
tar xf "$f"
done
which is unlikely to be noticeably slower than any attempt at using xargs
. In each case the process of spawning multiple tar
processes is likely to outweigh any differences in looping in bash
vs the internal loop in xargs
.