1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

xargs: A Powerful Command-Line Utility

Discussion in 'Unix' started by pradeep, Feb 21, 2012.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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 *
    bash: /bin/rm: Argument list too long
    
    You can solve it using xargs this way:
    Code:
    [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:
    Code:
    [pradeep@deepz-desktop ~]# cat test.txt
    pradeep sajal
    partha
    Anju    Asha
    
    Passes all strings as arugments to echo.
    Code:
    [root@techdev ~]# cat test.txt | xargs echo
    pradeep sajal partha Anju Asha
    
    Pass only one string at a time
    Code:
    [pradeep@deepz-desktop ~]# cat test.txt | xargs -n 1 echo
    pradeep
    sajal
    partha
    Anju
    Asha
    
    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.
    Code:
    [pradeep@deepz-desktop ~]# cat test.txt | xargs -n 1 -0 echo
    pradeep sajal
    partha
    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.

    Code:
    # 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.

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

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

    References



    http://en.wikipedia.org/wiki/Xarg
     
  2. ArihanT

    ArihanT New Member

    Joined:
    Mar 13, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    interesting read :)
     

Share This Page