Pages: Welcome | Projects

Wonders of xargs

2017/3/28
Tags: [ Hacking ] [ shell ]

The xargs software is way cooler that what it seems at the first glance. It does not simply flip lines from stdin to arguments in argv. It actually provides a really elegant way of having parallel job executions via the -L and -P flags.

For instance, we can split our input so that each invocation of the target program has only five lines...

seq 1 50 | xargs -L5 perl -wE 'say "$$ [@ARGV]"'

And we can state that there should be up to 4 instances of the target program, instead of just one (the default):

$ seq 1 50 | xargs -L5 -P4 perl -wE 'say "$$ [@ARGV]"'
29583 [15 16 17 18 19]
29582 [10 11 12 13 14]
29581 [5 6 7 8 9]
29584 [20 21 22 23 24]
29585 [25 26 27 28 29]
29586 [30 31 32 33 34]
29580 [0 1 2 3 4]
29587 [35 36 37 38 39]
29590 [50 51 52 53 54]
29589 [45 46 47 48 49]
29591 [55 56 57 58 59]