xargs: Obviously first

I feel strangely underqualified to mention xargs, even though it’s on my list as the first tool in the X section … once X itself is thrown out, of course. πŸ™„

I’ve known about and used xargs for the better part of a decade now, and it continues to crop up from time to time.

Strangely enough, I couldn’t spit out my own example of how to use it, even if I tried. It’s one of those things that’s around just enough to be useful, but not so often that I take the time to learn it right.

There are a few fun things that you do with xargs, that are worth mentioning.

2014-07-04-6m47421-xargs-01

The problem with that particular example is that you get the same results from

kmandla@6m47421: ~$  find music/ -maxdepth 1 -type d -exec echo "Directory: "{} \;

and so I can’t help but wonder what use it would be.

More helpful is the ability to pass specific information to other programs with xargs. For example. …

2014-07-04-6m47421-xargs-02

There, the results of ls are fed into wc via xargs. The problem again though, is that you get identical results from

kmandla@6m47421: ~/temp$ wc -l output-*

(Some restrictions may apply.) Which, at this point, probably makes you wonder what use xargs is, if so much of it is more easily done with the options available in other software.

Well here’s something that would be a little more tricky without xargs.

2014-07-04-6m47421-xargs-03

There, the -n 2 flag told xargs to lop off the results two at a time, and continue through the list to the end. So each pair of files gets run through wc, a total is shown, and then the next two are sent. An esoteric example, but nifty.

Here’s one more.

2014-07-04-6m47421-xargs-04

This time, xargs encountered the -t flag, which tells it to display the command it has received before showing the output. Useful for troubleshooting your command, or as a visual border for the results you get.

I’m sure you can come up with some more interesting and useful examples than these. Like I said, I encounter xargs just often enough to know what it does and why it’s necessary, but not enough to get into the nitty-gritty.

xargs is in findutils in both Arch and Debian. πŸ˜‰

Advertisements

4 thoughts on “xargs: Obviously first

  1. Pingback: Links 6/7/2014: Korea’s Domestic GNU/Linux Plans | Techrights

  2. Philipp Pirozhkov

    Xargs is capable running several commands in parallel. Nice to have if you have a file with a list of links and need to fetch them and start extraction before all of them are downloaded. Yep, you need to be a cli ninja to do that.

  3. Pingback: cliwiki: Needing attention | Inconsolation

  4. Pingback: ksmp3play: Simple and direct, but incomplete | Inconsolation

Comments are closed.