xargs command is used to build argument for and execute other utility commands. It is really useful command if you want to pass a list of output from other commands and use that output as an argument for other commands. In this post I am going to use most frequently used xargs command syntax with examples.

Examples

$ pwd
/Users/rahul/xargs-tutorial
$ echo 'one.txt two.txt three.java four.py' | xargs touch

above command will create four files named one.txt, two.txt, three.java and four.py in present working directory.

Note: The touch utility sets the modification and access times of files. If any file does not exist, it is created with default permissions.

$ ls
four.py		one.txt		three.java	two.txt

Remove files ending with ‘.txt’

$ find . -name "*.txt" | xargs rm
$ ls
four.py		three.java

Deleting files with whitespace in there name

$ find . -name "*.txt" | xargs rm

Above command will not delete files which has white space in there name, to delete those files use -print0 flag with find command and -0 flag with xargs

$ find . -name "*.txt" -print0 | xargs -0 rm

Prompt for user confirmation before executing the command

$ echo 'one.txt two.txt three.java four.py' | xargs touch
$ ls
four.py		one.txt		three.java	two.txt

use -p flag with xargs, it will prompt for user input(y/n) before executing the command. y will execute the command and n will do nothing. This is really helpful command if you want to execute some deletion operations and confirm before each command to be executed.

$ find . -name "*.txt" | xargs -p rm
rm ./two.txt ./one.txt?...n

Since I have passed n it will not remove the files

$ ls
four.py		one.txt		three.java	two.txt

Print command before execution

use -t flag with xargs to print each command before execution.

$ find . -name "*.txt" | xargs -t rm
rm ./two.txt ./one.txt

Using xargs with grep command

$ ls
five.java	four.py		three.java
$ cat three.java 
class Simple{  
    public static void main(String args[]){  
       System.out.println("Hello Java");  
    }  
}  
$ cat four.py
# This program prints Hello, world!

print('Hello, world!')
$ cat five.java
class HelloWorld
{
   public static void main(String args[])
   {
      System.out.println("Hello World");
   }
}

Grep ‘Hello’ in all the files ending with java

$ find . -name "*.java" | xargs grep Hello

output:
./three.java: System.out.println(“Hello Java”);
./five.java:class HelloWorld
./five.java: System.out.println(“Hello World”);