StumbleUpon – The Fickle Reader Has Challenged Me

by Tom Duffy

Lately, I have been getting a lot of traffic to my blog from StumbleUpon. This is great! Welcome fellow Stumblers! However, a few Stumblers have complained about my choice in titling my last blog entry with the word “Advanced” claiming that the information I was providing was more “Intermediate” than “Advanced”. Okay…I agree completely. The information I was providing was not very advanced. The reason I chose to name it “Advanced” was because of the massive influx of new Linux users who are not used to this sort of thing. To them, deactivating their NIC from the command line is “Advanced”.

This is all fine and well, but now I have this strange feeling in my gut. It’s a feeling that I used to get when I was dared to do something in elementary school. A feeling that I get when my knowledge or experience is challenged. So, now I feel I must redeem myself with a tutorial on what I would consider a much more “Advanced” Linux topic. So, without further adieu, I give you…xargs.

xargs – silly noob, commands are for piping.

Usually in a Linux or Unix environment, we run commands in order to get information in the form of an output. This output might be a list of strings for example. Then we read that information from the output in order to do something with it. Or we copy and paste part of the output into a new command. Wouldn’t it be easier to just use one command to do all of this for us? Wouldn’t it be easier to have a command’s output automatically used as a parameter for another command? Ladies and gentlemen, please welcome xargs. xargs allows you to execute some other commands on the output. For example, lets say that we want to find files in a directory that are symbolic links or are compressed. Then let’s say that we want search those symbolic links and compressed files for the word “foo”. And then let’s say that we want to have the output of all of this display nice and neat in the form of the ls command. Let’s try this:

file -Lz * | grep foo | cut -d":" -f1 | xargs ls -ltr

Let’s dissect this command string. The first, file -Lz *, finds files that are symbolic links or compressed. It passes the output to the next command, grep foo, which searches for the string “foo” in them and produces the output similar to this:

auth.log:              foo.techremedy.net
auth.log.1.GZ:      foo.techremedy.net (compress'd data 16 bits)

Since we are interested in the file names only, we applied the next command, cut -d”:” -f1, to show the first field only:

auth.log
auth.log.1.GZ

Now, we want to use the ls -l command and pass the above list as parameters, one at a time. The xargs command allowed you to to that. The last part, xargs ls -ltr, takes the output and executes the command ls -ltr against them, as if executing:

ls -ltr auth.log
ls -ltr auth.log.1.GZ

So, as you can see, xargs isn’t so much awesome on its own, but when used with other commands, it’s awesome factor increases significantly.

There is some good information out there about xargs. I recommend the man page. Just type man xargs and you can read it. One thing to point out is that white space, or blank spaces aren’t handled well by xargs at all. So, be sure to use the –null option or the -0 option to overcome this problem. I hope that this is helpful and “Advanced” enough for you fickle Linux folks out there. I will apologize for the unfortunate “Nitty Gritty Linux Hacking” part of the title on my last post. That was just uncalled for! :)



New From: $29.00 USD In Stock
Used from: $29.99 USD In Stock



List Price: $79.99 USD
New From: $11.93 USD In Stock
Used from: Out of Stock

  • Colton

    This isn’t advanced! This is just challenging! ;)

    Naw, I didn’t see your last post, but stumbled to this one, and I’ll agree that this one could be advanced. It actually didn’t seem advanced at first, but I’m fresh out of a semester of Unix programming, so I guess that’s to blame!

    It’s interesting how learning more doesn’t make you feel like you personally know more as much as it makes other things seem less difficult. Anyway, keep up the good work!