Go4Expert (http://www.go4expert.com/)
-   Unix (http://www.go4expert.com/articles/unix/)
-   -   xargs: A Powerful Command-Line Utility (http://www.go4expert.com/articles/xargs-powerful-command-line-utility-t27850/)

pradeep 21Feb2012 19:24

xargs: A Powerful Command-Line Utility
xargs is an utility which is used to build & execute command-lines. xargs reads from the standard input (STDIN) space, tab, newline & end-of-file (EOF) delimited strings and executes the specified utility with the strings as argument(s). Most utilities/systems have a limit on the maximum no. of arguments that can be passed, so if you have a very long list of hundreds of arguments xargs can help, you might think why the hell would anyone want to pass so many arguments, then look at this scenario:

[pradeep@deepz-desktop /tmp]# rm *
bash: /bin/rm: Argument list too long

You can solve it using xargs this way:

[pradeep@deepz-desktop /tmp]# ls -1 * | xargs rm
xargs splits the output from ls command into acceptable no. of arguments and passes it to rm.

There are a variety of options which allow you to tweak it to your needs, which we'll look into in the examples.

When To Use xargs

In simple words you need to use xargs when you want to use output of a command/contents of a file and pass it as arguments, either per-line or delimited by space, tab or EOF. See a demonstration below:

Most of you must be familiar with the echo command, it takes a string as input and just prints it to the standard output (STDOUT).

Contents of the file:

[pradeep@deepz-desktop ~]# cat test.txt
pradeep sajal
Anju    Asha

Passes all strings as arugments to echo.

[root@techdev ~]# cat test.txt | xargs echo
pradeep sajal partha Anju Asha

Pass only one string at a time

[pradeep@deepz-desktop ~]# cat test.txt | xargs -n 1 echo

Tell xargs to use NUL character as delimiter instead of space & newline, this causes the file to be printed as-is but if you notice the output has 3 extra blank lines in the end.

[pradeep@deepz-desktop ~]# cat test.txt | xargs -n 1 -0 echo
pradeep sajal
Anju    Asha

[pradeep@deepz-desktop ~]#

Some Practical Uses of xargs

xargs can run parallel jobs and these days most of us have multicore CPUs, so say you would like to convert a directory full of uncompressed loss-less audio files to a compressed format. Say, I have a dual core CPU I want to run 2 processes simultaneously.


# find . -type f -name '*.wav' -print0 |xargs -0 -P 2 -n 1 flac -V8
Kill processes matching some pattern. In the example I would like to kill all httpd processes.


# ps ax | grep httpd | awk '{print $1}' | xargs kill -15
Find all text files and add them to a TAR archive.


# find ~ -name '*.txt' -type f -print | xargs tar -cvzf my_text_files.tar.gz



ArihanT 13Mar2012 17:58

Re: xargs: A Powerful Command-Line Utility
interesting read :)

All times are GMT +5.5. The time now is 03:37.