Xargs  is a powerful command that takes input from the output of another command and pass them as argument of another command in an effective way. Here is some of the examples which will guide you to learn this awesome command.

1) The output of first two commands is exactly same. But in the second command we piped the output of `echo` to xargs.

This shows that if xargs is devoid of arguments then it just forwards the input.

 ➜  $  echo file{0..9}.txt
file0.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt

 ➜  $  echo file{0..9} | xargs 
file0.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt

2) Here I introduce an option -n MAX_ARGS

➜  $  echo file{0..9} | xargs -n1
file0
file1
file2
file3
file4
file5
file6
file7
file8
file9

-n1 option splits the input stream with 1 argument at a time. Similarly the output for -n2 and -n3 is :

➜  $  echo file{0..9} | xargs -n2
file0 file1
file2 file3
file4 file5
file6 file7
file8 file9

➜  $  echo file{0..9} | xargs -n3
file0 file1 file2
file3 file4 file5
file6 file7 file8
file9

3) Now, we will try to create files with the filenames generated by echo command.

➜  $  echo file{0..9}.txt | xargs touch
➜  $  ls
file0.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt

Another usage :
Here each line of filenames.txt contains a file name needs to be created.

➜  $  cat filenames.txt | xargs -n1 touch

4) The following examples makes a tar ball of the files it gets from the pipe stream.

➜  $  echo file{0..9}.txt | xargs tar -cvfz files.tar.gz 
➜  $  find . -name '*.txt' -type f | xargs tar -cvfz files.tar.gz

This  makes a tar ball of text files in the current directory.

5) The following command is very effective to know disk usage of your system.

`du -sh ` is used for finding disk usage, option -s for summary and -h for human readable format.

➜  $ cd /
➜  $ sudo ls / | xargs -n1 sudo du -sh 
9.1G     Applications
4.3G     Library
1.0K     Network
3.9G     System
du: User: No such file or directory
du: Information: No such file or directory
 65G     Users
4.0K     Volumes
3.0M     bin
  0B     cores
4.5K     dev
4.0K     etc
1.0K     home
7.9M     mach_kernel
1.0K     net
1.8G     opt
 14G     private
1.5M     sbin
4.0K     tmp
5.7G     usr
4.0K     var

6) But the output is not what we expected, for a directory `User Information` xargs treated `User` and `Information` separately. So what is the solution?

Here, I am introducing another option -I [replace_string].
This replace occurrences of [replace_string] with the input, and for -I separator is newline character.

➜  $  ls  | xargs -I {} sudo du -sh {}
or 
➜  $  ls  | xargs -I folder sudo du -sh folder
9.1G     Applications
4.3G     Library
1.0K     Network
3.9G     System
4.0K     User Information
 64G     Users
4.0K     Volumes
3.0M     bin
  0B     cores
4.5K     dev
4.0K     etc
1.0K     home
7.9M     mach_kernel
1.0K     net
1.8G     opt
 14G     private
1.5M     sbin
4.0K     tmp
5.7G     usr
4.0K     var

7) If you have encountered `command: arg list too long` in your shell script; xargs is the obvious solution.

Note:

➜  $ getconf ARG_MAX
 262144

So the maximum number of argument it can process is 262144. Sometimes this size seems to be small. Specially if you are trying to delete a game folder containing almost a million files.

Simple command to delete all the html files from present working directory.

➜  $  find . -name ‘*.html’ -print0 | xargs -0 rm -f

-0 If there are blank spaces or characters (including newlines) many commands will not work. This option take cares of file names with blank space, The `-print0` option lists the found files separated not with a newline but with a null (or “NUL”) character, which is not a legal character in Unix or Linux file names.

8) What if multiple lines from input stream is needed to be used as a single argument for the command used in xargs.
The solution is -L[num_lines] options

➜  $ ls -l | cut -d' ' -f9-12
Dec 28 22:05 1.txt
Dec 28 22:05 10.txt
Dec 28 22:05 11.txt
Dec 28 22:05 12.txt
Dec 28 22:05 13.txt
Dec 28 22:05 14.txt
Dec 28 22:05 15.txt
Dec 28 22:05 2.txt
Dec 28 22:05 3.txt
Dec 28 22:05 4.txt
Dec 28 22:05 5.txt
Dec 28 22:05 6.txt
Dec 28 22:05 7.txt
Dec 28 22:05 8.txt
Dec 28 22:05 9.txt
➜  $  ls -l | cut -d' ' -f9-12 | xargs -L4 echo
Dec 28 22:05 1.txt Dec 28 22:05 10.txt Dec 28 22:05 11.txt Dec 28 22:05 12.txt
Dec 28 22:05 13.txt Dec 28 22:05 14.txt Dec 28 22:05 15.txt Dec 28 22:05 2.txt
Dec 28 22:05 3.txt Dec 28 22:05 4.txt Dec 28 22:05 5.txt Dec 28 22:05 6.txt
Dec 28 22:05 7.txt Dec 28 22:05 8.txt Dec 28 22:05 9.txt

Once you master the basic usage of xargs command that I’ve explained above, you can wisely use xargs command to solve lot of your argument manipulation requirements for processing other commands.

Pic Courtesy: allacronyms.com