Xargs is one of the oddest yet most powerful commands available to the shell. It reads lines of text from standard imput,turns them into commands and executes them.

Well, you might say, other command or tools can do the same thing too. But, xargs has some unique uses, particularly for processing a list of fils/items you have, and expecially useful if you combine with other commands and scripts.

For example:

1. Manage multiple processes on Linux

$find ./x* -print0 | xargs -0 -n 1 -P 5 bzip2

The xargs example above will find list of files, then feed to xargs to execute 5 bzip2 processes in parallel. -n means feed one file(line) to each bzip2 process.

Note: -n, -P -0 options will be explained in later examples.

2. -0 option, Useful when input items might contain white space, quote  marks,  or  backslashes

Quite common some file name contains blank space, then the following command will take wrong action.

$find ./x* | xargs rm -f

-0 option expects ASCII nulls as item termination which -print0 feeds, rather than whitespace, and treat all characters literally.

3. -n k , feed k lines of input to the command being executed, default is 1

$ls /usr/bin/* |xargs -n 1 echo 
/usr/bin/[
/usr/bin/2to3
/usr/bin/411toppm
/usr/bin/7za
...

$ls /usr/bin/* |xargs -n 2 echo
/usr/bin/[ /usr/bin/2to3
/usr/bin/411toppm /usr/bin/7za
/usr/bin/a2p /usr/bin/a2ping
...

4. -P option, Run up to max-procs processes at a time

Note just other simple commands, xargs also can execute other script/programs with feeding items

$for i in `seq 1 100` | xargs -n 1 -P 5 mytask 

In the example above, xargs run up to 5 mytask processes.

5. -I options, replace string usage

The options replace occurrences of replace-str in the initial-arguments with names  read  from  standard input.

More easier to understand it with example

xargs -I'{}'/ mv dir1/'{}' dir2/'{}'

6. Process input items are terminated by the specified  character

By default, white space, quote, and back slash are treated as item terminations, with this option, you can specify your own character.

7. -L, -s options for feeding control to xargs

      -L max-lines
              Use  at  most  max-lines  nonblank input lines per command line.
              Trailing blanks cause an input line to be logically continued on
              the next input line.

       -s max-chars
              Use at most max-chars characters per command line, including the
              command and initial-arguments and the terminating nulls  at  the
              ends of the argument strings.  The largest allowed value is sys-
              tem-dependent, and is calculated as the  argument  length  limit
              for  exec, less the size of your environment, less 2048 bytes of
              headroom.  If this value is more than 128KiB, 128Kib is used  as
              the  default value; otherwise, the default value is the maximum.
              1KiB is 1024 bytes.

They both for per command line control, less likely xargs encounter this type of problem.

But, if you might not  suprised to see error in the following commands if the list of files are too long.

$rm -f  `cat file.lst`
$ls `cat file.lst`

But, xargs is perectly ok with it

$cat file.lst | xargs rm -f