xargs is fireworks for your shell commands.

Learn to harness this great power of xargs today!

The information presented here is based on research and many years of experience using this particular method and approach.

Four hard drivers sticking out of a rack-mount server that’s being used as a NAS.

Pipes and xargs

Anyone learning Bash will grow in their command line scripting skills over time.

The syntax is as good as identical.

And, as skills improve, newer engineers will usually discover the Pipe idiom first.

Terminal window showing the manual page for the Mutt email client on Linux

For example, consider this situation:

Here we start a sleep in the background.

The problem is thatkilldoes not accept the input directly, whether it comes frompsor even from a simpleecho.

It is thus as if we executedkill -9 some_piddirectly.

Article image

Shutterstock/ksb

One small change to code (i.e.

Still, even this has it’s limitations.

How about if we wanted to provide some nice debug information printed inline and from within the statement?

A simple Bash pipe example with sed regex text replacement

It seems impossible thus far.

Let’s take our previous example and inject some debugging information without post-processing the outcome.

There is a lot of power here.

Running into challenges using a pipe in combination with kill

Otherwise, if the secondechowas not there, the firstechowould start to output ‘The PID …’ etc.

We first create three files, a, b and c, and a subdirectory named subdir.

We can also see how there are no more files in the subdir.

xargs resolving the pipe kill command issue seen earlier

Wrapping up

Usingxargsbeing great power to an advanced Linux (and in this case) Bash user.

A complex xargs command build sequence and using bash -c

A fully working mini script based on xargs and bash -c in a one-liner command