xargs command in Linux with examples

24 May, 2019
Importance :Some commands like grep can accept input as parameters, but some commands accepts arguments, this is place where xargs came into picture.
Syntax :
xargs [options] [command]

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
Example :
xargs example
Example :Below is the C program, which reads a text file “test.txt” and then use output of this program as input to touch command.
contents of text file “test.txt”
file1
file2
file3
file4

// C program to read contents of file#include <stdio.h>  // Driver Codeint main(){    int c;    FILE *file;      // open file test.txt    file = fopen("test.txt", "r");    if (file) {          // read file line-by-line until        // end of file        while ((c = getc(file)) != EOF)            putchar(c);          fclose(file);    }    return 0;}
Output :
file1
file2
file3
file4

Now, use output of ./a.out as input to touch command
xargs example with touch
Command usage with options :
xargs --version

Prints the version number of xargs command and then exit.
Output :
xargs (GNU findutils) 4.7.0-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

xargs -a test.txt

It will show contents of file
file1
file2
file3
file4

xargs -p  -a test.txt

-p option prompts for confirmation before running each command line. It only runs the command line if the response starts with ‘y’ or ‘Y’
Output :
# xargs -p  -a test.txt
echo file1 file2 file3 file4 ?...y
file1 file2 file3 file4

# xargs -p  -a test.txt                                                                                                                 
echo file1 file2 file3 file4 ?...n

 xargs -r -a test.txt

Now, let’s suppose the file “test.txt” is empty, and above command is executed, -r option ensures if standard input is empty, then command is not executed, so above command will not produce any output,But, if above command is executed without -r option, it will produce a blank line as output.See below image as instance :
xargs with -r option
– Mandeep Singh
References :1) xargs wikipedia2) need of xargs3) xargs man page
linux-command
Linux-Shell-Commands
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Docker - COPY Instruction
scp command in Linux with Examples
chown command in Linux with Examples
Introduction to Linux Operating System
SED command in Linux | Set 2
Array Basics in Shell Scripting | Set 1
nohup Command in Linux with Examples
chmod command in Linux with examples
mv command in Linux with examples
Basic Operators in Shell Scripting