![]() |
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:
Code:
[pradeep@deepz-desktop /tmp]# rm *Code:
[pradeep@deepz-desktop /tmp]# ls -1 * | xargs rmThere 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 xargsIn 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: Code:
[pradeep@deepz-desktop ~]# cat test.txtCode:
[root@techdev ~]# cat test.txt | xargs echoCode:
[pradeep@deepz-desktop ~]# cat test.txt | xargs -n 1 echoCode:
[pradeep@deepz-desktop ~]# cat test.txt | xargs -n 1 -0 echoSome Practical Uses of xargsxargs 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. Code:
# find . -type f -name '*.wav' -print0 |xargs -0 -P 2 -n 1 flac -V8Code:
# ps ax | grep httpd | awk '{print $1}' | xargs kill -15Code:
# find ~ -name '*.txt' -type f -print | xargs tar -cvzf my_text_files.tar.gzReferenceshttp://en.wikipedia.org/wiki/Xarg |
Re: xargs: A Powerful Command-Line Utility
interesting read :)
|
| All times are GMT +5.5. The time now is 19:14. |