June 19, 2016

Using Xargs | Superuse your Terminal

02:37 Posted by DurgaSwaroop , , , , No comments
Xargs is one of the really useful Linux commands that every one should know about. It is a great command that simplifies your task at hand and overall, a  great command to  have in your arsenal. And, its quite simple to use.

So, What is xargs?
Xargs is a command that helps you run a command on a series of things in order.  The easiest way to understand this is by looking at an example. Before, that here is the typical command structure when you use xargs.

<Some Input generator> | ... | xargs <command to run>

Almost all the time xargs gets its input piped from another command but remember that it can also accept input directly from the keyboard.
Now, Let's look at an example on what xargs can do.
This command will print 1 -5 numbers as one would expect,
$ echo 1 2 3 4 5
1 2 3 4 5
Now, Let's say I want to print a 'Hello' in front of every number in the output of the previous command.
So we do,
 $ echo 1 2 3 4 5 | xargs echo Hello
We get,
 Hello 1 2 3 4 5
That is clearly not what we were expecting.
So,what's the deal here? Well, Here the input is coming as a single row and so xargs runs the  command only once per each row unless you tell it otherwise. So, to have xargs put 'Hello' infront of  every number, we have to pass an option like this.
 $ echo 1 2 3 4 5 | xargs -n 1 echo Hello
And, now as you can see we got it.
Hello 1
Hello 2
Hello 4
Hello 3
Hello 5
So, by adding this option we are telling xargs to run the echo Hello command once for every number it gets as input. (Default delimiter being the white space)
Now, similarly if we do -n 2
$ echo 1 2 3 4 5 | xargs -n 2 echo Hello
Hello 1 2 Hello 3 4 Hello 5
You get this since echo Hello is getting executed once for every two numbers. And, you can extend the similar approach further.

Now, you probably  might be thinking, this is all good but where can I really use it? Well, we are going to look at some pretty useful practical use cases that will convince you to using xargs.

Imagine,  you have got a rogue process that has spawned several threads and you want to kill'em all. Now instead of manually doing kill <PID> on every process thread,  you can use xargs to do it for you
 $ ps aux | grep "rogue_process_name" | cut -c 4-9 | xargs kill

And, this is going to kill all the processes with that name. And, 4-9 is the PID. Super cool right?!
Let's look at another possible scenario. Imagine you are looking for a file named 'MysteryX' and are for each file with that name you want to see its permissions. Xargs to the rescue again..
 $ find -name "MysteryX" | xargs ls -lh

And, you have everything you need to know about those files. Similarly you can find the word counts of all of them or you can do a batch delete or batch chmod on all the matching files at once. That is certainly going to speed up your work flow.
Another useful example would be for mass downloads. Let's say you have a site with videos you want to download. Instead of downloading them individually you can have a script parse that page and give out all the links in it and then you can run an xargs wget -c  on those links to download them right away.
So, that is all for now to get you started with using Xargs. For more info on xargs refer to the man pages or go to Xargs - Linux Man Page
Keep Practicing and Keep Learning!

If you have liked this article and would like to see more, subscribe to our Facebook and G+ pages.
Facebook page @ Facebook.com/freblogg
Google Plus Page @ Google.com/freblogg


Post a comment

Please Enter your comment here......