The xargs Linux command is an extremely useful command-line tool, especially when combined with other commands like find and grep. If xargs is new for you this post will help you understand the usage of xargs.

xarg is a Unix command which is used to build and execute commands from standard input.If no command is supplied as an argument to xargs, the default command that the tool executes is echo

Syntax :

xargs [options] [command [initial-arguments] ]

xargs options :

-0 : input items are terminated by null character instead of white spaces
-a file : read items from file instead of standard input 
–delimiter = delim : input items are terminated by a special character
-E eof-str : set the end of file string to eof-str
-I replace-str : replace occurrences of replace-str in the initial arguments with names read from standard input
-L max-lines : use at-most max-lines non-blank input lines per command line.
-p : prompt the user about whether to run each command line and read a line from terminal.
-r : If the standard input does not contain any nonblanks, do not run the command
-x : exit if the size is exceeded.
–help : print the summary of options to xargs and exit
–version : print the version no. of xargs and exit

How to use xarg commands 

Xargs reads arguments from the standard input, separated by blank spaces or newlines, and executes the specified command using the input as command’s arguments. If no command is provided, default is /bin/echo.

The xargs command  will pass several strings separated by whitespace using a pipe symbol to xargs and run a command that will use those strings as arguments.

echo "codesposts.txt" | xargs touch

The above command will create a text file using touch command and makes the file name as codespost.txt. This command will be same as we execute

Touch codesposts.txt

Use xargs to trim whitespaces

Xargs can be handy when you need to clear whitespaces in your text.

echo "  Codes posts " | xargs
Output:
Codes posts 

This command has removes the whitspaces before and after of a given string.

Xargs with delimiter -d

Delimiters is applied to take each character in input using -d option in xargs command.
In this example,the -d, i.e comma delimiter is used, and display the output as a comma separated field
In this example,the -d, i.e comma delimiter is used, and display the output as a comma separated field

$ echo "codes,posts,xargs" | xargs -d, -L 1 echo
Output:
codes
posts 
xargs

Xargs to limit Output Per Line Using -n 

xargs command shows whatever indput given as shown below.

$ echo a b c d e f| xargs
Output:
a b c d e f

The output of the xargs is split into multiple lines using -n option.
In the below example, we we are going to display 3 words per line so we are giving the value -n 3

$ echo a b c d e f| xargs -n 3
Output:
a b c
d e f

we can also split 2 items per line by the following example

$ echo a b c d e f| xargs -n 2
Output:
a b
c d
e f

Xargs to Prompt User Before Execution using -p option

The -p in xargs option will allow you to confirm the command before execution.if we want to confirm each execution by the user we can do it by the following example.

$ echo code post prompt example by user | xargs -p -n 3
Output:
/bin/echo code post prompt ?...y
/bin/echo example by user ?...y
code post prompt
example by user

In the following example, we disallowed by giving n so the xargs command did not execute anything

$ echo code post prompt example by user| xargs -p -n 3
Output:
/bin/echo code post prompt ?...n
/bin/echo example by user ?...n
/bin/echo ?...n


Note: This is helpful when you are combining xargs with commands that are disruptive like rm. In those cases, you may want to see what xargs does.

Xargs with Find Command

This is the most common and one of the important usage of xargs command in real time. When you need to find certain type of files and perform certain actions on them (most popular being the delete action). The xargs command gets its purpose when we use it with other commands.
In this example, the output of the find command is all the files with *.c extension, which is given as input to the xargs command, which in-turn execute “rm -rf” command on all the *.c files.

$ ls
one.c  one.h  two.c  two.h
$ find . -name "*.c" | xargs rm -rf
$ ls
one.h  two.h

Xargs to compress files with same extensions

This example shows us how to find all the .txt files and archive them using the tar utility as follows.

Here, the action command -print0 enables printing of the full file path on the standard output, followed by a null character and -0 xargs flag effectively deals with space in filenames.

$ find files/codesposts/ -name "*.txt" -type f -print0 | xargs -0 tar -cvzf txt.tar.gz

The same can be used to do similar tasks for different extension and commands.

Xargs example to read from file

The xargs command can read items in a file. To do so use the -a (–arg-file) option followed by the name of the file..

In the following example, the xargs will read the ips.txt file and ping each IP Address.

We are also using the -L 1 option which instructs xargs to read one line at the time. If this option is omitted xargs will pass all IPs to a single ping command.

xargs -t -L 1 -a ips.txt ping -c 1
Output:
ping -c 1 8.8.8.8 
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=50 time=68.1 ms
...
ping -c 1 1.1.1.1 
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=59 time=21.4 ms

Xargs to find number of lines/words/characters in each file of directory

We have a list of files, and we want to know the number of lines/words/characters in each file in the directory, you can combination of ls and xargs to make this possible.

$ ls *codesposts* | xargs wc

By now you should have got an basic understanding about Xargs.To understand more detailed information about xargs , read the xargs man page.

$ man xargs