Linux command xargs reads items from the standard input, delimited by blanks or newlines ,then run command followed by the items read from standard input.

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

Below is some examples.

Exampe 1 : Replace new line with space using xargs

j@ubuntu2004:~$ seq 3
1
2
3
j@ubuntu2004:~$ seq 3 | xargs
1 2 3

since the default command is “/bin/echo” , so xargs replaces “new line” with space

Example 2 : Find particular files then remove them

find / -name log -type f -print | xargs /bin/rm -f

Exmaple 3 : Run command in parellel use xargs

Use option “-P N” can run command in parellel, “N” number of processes in parellel.

j@ubuntu2004:~$ yes 5 | head -10
5
5
5
5
5
5
5
5
5
5
j@ubuntu2004:~$ time yes 5 |head -10 | xargs sleep

real	0m50.006s
user	0m0.003s
sys	0m0.004s
j@ubuntu2004:~$ time yes 5 |head -10 | xargs -n 1 -P 10 sleep

real	0m5.010s
user	0m0.014s
sys	0m0.003s

We can see with option “-n 1 -P 10” 5 instead of 50 seconds is used.

Example 4 : xargs string substitution example using option “-I”

j@ubuntu2004:~$ cat hosts.txt  | xargs -I '{}'  ssh '{}' uptime

       -I replace-str
              Replace occurrences of  replace-str  in  the  initial-arguments  with
              names  read from standard input

Above example read home name from hosts.txt then ssh to them to get uptime information