12
Sep
07

xargs.. Life can be much easier!

Any Linux admin has a special love story with xargs, It’s like a true friend. It doesn’t always appear to be handy until you stuck looking for a quick and nifty solution. One of these situations is when you need to filter a certain directory that contains a lot of files and you need also to manipulate those files.

xargs is an execution command, you pass your arguments and it will take care of the execution based on the given commands. For instance, I need to get the number of lines in each file within the current path.

ls | xargs wc -l

ls0.png

Simple, Isn’t it?

The first part of this command is very basic, you list the contents of the directory. Which apparently shows the following output:

basicxargs0.png

The second part is the real crux! Here, each line of the ls output will be piped as a standard stream to the xargs which will be process each line accordingly. This means the wc -l will be executed for each line, as if you would write it this way:

wc -l hello.c hello.ko hello.mod.c hello.mod.o hello.o
.
..

And so on!

Internally, xargs command tears up its Stdin input to separate lines based on a delimited character. Most likely, It’s a whitespaces, tabs, new lines and EOF.

xargs-internals.png

A very useful way to check and debug the execution pipeline within xargs is to add the option -t:

ls | xargs -t wc -l

The result of adding the -t option would be:

wc -l 1 anaconda-ks.cfg apache_1.3.37 apache_1.3.37.tar.gz Desktop hello.c hello.ko hello.mod.c hello.mod.o hello.o index.html install.log install.log.syslog InstallShield JavaApplication1 jdk1.5.0_09 jdk-1_5_0_09-nb-5_5-linux.bin lighttpd-1.4.16 lighttpd-1.4.16.tar.gz lynx-2.8.3-1.i386-cygwin.rpm lynx-debuginfo-2.8.5-18.0.2.i386.rpm Makefile mysql-5.0.41.tar.gz php-4.4.6.tar.gz rpm temp temp.c

I guess it’s pretty much meaningful now, Isn’t it?

Let’s heat this up a little bit:

You can use the blackslah “\” to write commands in multiple lines, the same example can be written as follows:

lsbackslash.png

“Don’t worry about .serverauth.XXXX files, just an XServer thing. It’s OK!” 

A powerful aspect of xargs is the ability to control the input data “stdin” from the pipe per invocation. As an example, we can limit the number of delimited strings to accept only two arguments “Again, per invocation”:

xargs-n.png

In shell world, you’re the king! xargs can ask for you permission for any execution “y = affirmative response, others are negative!. How easy!!”. The option “-p” takes care of this:

xargs-p2.png

If you feel playful a little bit, I guess it’s your time to enjoy. Manipulation moments are pretty much exciting! As you have formerly seen, xargs depends largely on the stdin “Standard Input” and if you don’t have enough control over that, xargs would be vain. Luckily, the option “-I” is a drastic option to pass the pipe’s output to the execution command within xargs. To make things more comprehensible, suppose that we need to copy all files within a certain directory to a different directory as a backup files “.bak”.

xargs-icp.png

Let’s break down that command, the “-I” option will determine the replacement string “‘%’”. A neat and tricky question can be raised right here:

Does the “-I” option include the single quote in the replacement string as a variable or is it considered as a escaping character?

The answer can simply be: YES! anything after “-I” and before a single/double unquoted whitespace is considered a replacement string. So, we may have the following valid strings:
‘%’
“‘%’”
%

But please don’t try this one:
HI!

At work I use “Linux~GNU”, but at home I use “*BSD~BSD”… In anyway, take care and note that GNU xargs uses “-i” and BSD version uses “-I”.


0 Responses to “xargs.. Life can be much easier!”



  1. No Comments Yet

Leave a Reply