xargs command in Linux with examples
Last Updated :
19 Jul, 2024
xargs is a Unix command which can be used to build and execute commands from standard input.
Importance:
Some commands like grep can accept input as parameters, but some commands accept arguments, this is a place where xargs came into the picture.
Syntax of `xargs` command in Linux
xargs [options] [command]
Options Available in `xargs` command in Linux
Options | Description |
---|
-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 Below is the C program, which reads a text file "test.txt" and then uses the output of this program as input to touch command. contents of text file "test.txt"
file1
file2
file3
file4
C
// C program to read contents of file
#include <stdio.h>
// Driver Code
int 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 touchCommand 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
Similar Reads
wc command in Linux with examples wc stands for word count. As the name implies, it is mainly used for counting purpose.It is used to find out number of lines, word count, byte and characters count in the files specified in the file arguments.By default it displays four-columnar output.First column shows number of lines present in a
6 min read
Wget Command in Linux/Unix Wget is the non-interactive network downloader which is used to download files from the server even when the user has not logged on to the system and it can work in the background without hindering the current process. GNU wget is a free utility for non-interactive download of files from the Web. It
6 min read
whatis Command in Linux with Examples whatis command in Linux is used to get a one-line manual page description. In Linux, each manual page has some sort of description within it. So, this command search for the manual pages names and show the manual page description of the specified filename or argument. Syntax of the `whatis` command
5 min read
How to Display Path of an Executable File in Linux | Which Command In Linux finding the exact path of an excutable file can be crucial for the system adminstration, scripting and as well for troubleshooting. The `which` command helps with providing a simple and effective way to locate the executable files within the directories that are listed in your system. In th
6 min read
while Command in Linux with Example The "while" command is a powerful tool in the Linux shell that allows you to execute a series of commands repeatedly based on a given condition. It is commonly used in shell scripts to create loops, where a block of code is executed as long as a particular condition remains true. Basic Syntax of Whi
6 min read
How to Display the current Username in Linux | whoami Command Imagine you're working on your computer and forget who you're logged in as. In Linux, there's a special trick called "whoami" that's like asking "Hey computer, who am I right now?" This article explains how this simple command works and helps you remember who's in charge! Don't worry, it won't be fu
4 min read
write command in Linux with Examples The `write` command in Linux facilitates the users in sending the messages directly to other logged-in users' terminals. For using this command, try on to type `write username` and then type your message. After typing your message, press `Ctrl+D` to end the session. This command is useful for quick
6 min read
xargs command in Linux with examples xargs is a Unix command which can be used to build and execute commands from standard input. Importance:Some commands like grep can accept input as parameters, but some commands accept arguments, this is a place where xargs came into the picture. Syntax of `xargs` command in Linuxxargs [options] [co
5 min read
xdg-open command in Linux with Examples xdg-open command in the Linux system is used to open a file or URL in the userâs preferred application. The URL will be opened in the userâs preferred web browser if a URL is provided. The file will be opened in the preferred application for files of that type if a file is provided. xdg-open support
2 min read
yes command in Linux with Examples yes, the command in Linux is used to print a continuous output stream of a given STRING. If STRING is not mentioned then it prints 'y'; Syntax: of `yes` command in Linuxyes [STRING] Note: To stop printing please press Ctrl + C. Use of `yes` commandlet us say that we want to delete all the `.txt` fil
2 min read