Open In App

xargs command in Linux with examples

Last Updated : 19 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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 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 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

xargs command in Linux with examples – FAQs

What is the xargs command used for?

The xargs command in Linux is a powerful utility that reads streams of data from standard input and converts them into arguments for a command. It is commonly used to build and execute command lines from standard input. xargs is especially useful for dealing with a large number of inputs that might otherwise exceed the command line’s maximum argument length, or when you want to apply a single command to a series of arguments iteratively.

How to use xargs with the find command?

Combining xargs with the find command is a common use case that allows you to perform operations on a large set of files returned by find. For example, to find all .txt files in the current directory and then delete them, you can use:

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

Here, find generates a list of .txt files, and xargs takes that list and feeds it to the rm command to delete the files.

Can xargs be used to run multiple commands?

Yes, xargs can be used to run multiple commands on the arguments it processes. This can be done by using a shell within xargs. For example, if you want to find all .txt files and then both move and copy them, you could use:

find . -name "*.txt" -print | xargs -I {} sh -c 'cp {} {}.backup; mv {} new_location/'

In this command, -I {} replaces {} with each filename found. The sh -c allows the execution of a compound command.

How to pass arguments from a file to xargs?

To pass arguments from a file to xargs, you can pipe the contents of the file into xargs. For example, if you have a file list.txt with one filename per line, and you want to delete all these files, you can use:

cat list.txt | xargs rm

Alternatively, xargs can read items directly from a file using the -a option:

xargs -a list.txt rm

What are some common options for the xargs command?

  • -n [max-args]: Specifies the maximum number of arguments taken from standard input for each command line. For instance, xargs -n 3 would use three arguments at a time.
  • -I [replace-str]: Specifies a replacement string. Any occurrences of replace-str in the initial-arguments will be replaced with names read from standard input. Commonly used as xargs -I {}.
  • -0 or --null: Input items are terminated by a null character instead of by whitespace, useful with input from find ... -print0.
  • -p or --interactive: Prompts the user before executing each command.
  • -d [delimiter]: Specifies a custom delimiter, which by default is whitespace. For example, using newline as a delimiter can be achieved by xargs -d '\n'.
  • -L [number]: Use at most number non-blank input lines per command line.
  • -t or --verbose: Print the command line on the standard error output before executing it.


Next Article

Similar Reads

three90RightbarBannerImg