Examples on how to use xargs command on Linux

The xargs Linux command allows a user to execute command lines from standard input. If that sounds confusing, it might be easier to look at a basic example. The following command would use xargs to cat all the files listed by the ls command.

$ ls
1.txt  2.txt  3.txt

$ ls | xargs cat
this is file1
this is file2
this is file3

In the command above, we piped the output of the ls command to xargs, and used the cat command to list the contents in each of the three files. Most uses of xargs involve piping to it from a different command, and utilizing it to execute another command in succession. If you want to start off with additional basic examples, check our guides on xargs for beginners or multi-threaded xargs examples.

In this guide, we’ll go through various command line examples of the xargs command, so you can learn how to use it effectively on a Linux system. Follow along as we go over the basics and more complicated aspects of the xargs command.

In this tutorial you will learn:

  • How to use xargs command on Linux, through examples
Various xargs command examples on Linux

Various xargs command examples on Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software xargs command
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

xargs command examples



Follow along with the examples below to see how to use xargs in various scenarios on Linux.

  1. Let’s go back to our original example to explain exactly what’s happening when we execute the xargs command. Right now we have three text files, and the goal is to view the contents of all three with the cat command. As these are the only three files in our current directory, we can use the ls command to retrieve the file names, and then pass those names to cat by using xargs.
    $ ls | xargs cat
    this is file1
    this is file2
    this is file3
    

    What’s really happening here is equivalent to the following command:

    $ cat 1.txt 2.txt 3.txt 
    
  2. To see the command xargs is executing, you can use the -t option. This will still execute the command, but it gives us a way to see how the final command has actually been formatted.
    $ ls | xargs -t cat
    cat 1.txt 2.txt 3.txt 
    this is file1
    this is file2
    this is file3
    
  3. We can also use the -p option, which will ask us for confirmation before executing the command. This is handy to use, for instance, when executing the rm command with xargs, since you wouldn’t want to accidentally delete any files. You can enter either y (yes) to confirm running the command, or n (no) to abort it.
    $ ls | xargs -p rm
    rm 1.txt 2.txt 3.txt ?...y
    


  4. In our previous example, xargs is executing cat 1.txt 2.txt 3.txt. In other words it’s only executing a single command and all the file names are being appended. We can make xargs run each cat command separately by using the -n (max arguments) option. Here’s an example where we’ve also included the -t option, so we can see how this command is executing cat three separate times, as opposed to just one time.
    $ ls | xargs -t -n 1 cat
    cat 1.txt 
    this is file1
    cat 2.txt 
    this is file2
    cat 3.txt 
    this is file3
    

    The -n 1 option in this case is telling xargs that it can only use one file name at a time (maximum). Here’s how it would differ if we used -n 2 instead.

    $ ls | xargs -t -n 2 cat
    cat 1.txt 2.txt 
    this is file1
    this is file2
    cat 3.txt 
    this is file3
    
  5. xargs can also run multiple commands if you use the -I option. You need to specify a “replace string” to use with this option. A very common one to use is a percentage sign, which we use below. For a simple example, the following command will execute touch and then cat on our three files, whose names are passed to xargs from ls.
    $ ls | xargs -t -I % sh -c '{ touch %; cat %; }'
    sh -c '{ touch 1.txt; cat 1.txt; }' 
    this is file1
    sh -c '{ touch 2.txt; cat 2.txt; }' 
    this is file2
    sh -c '{ touch 3.txt; cat 3.txt; }' 
    this is file3
    
  6. Another handy use of xargs is its ability to accept input from files. This is done with the -a option. Take the following command for example where we pass a list of file names to xargs, and then cat those files.
    $ xargs -t -a files.txt cat
    cat 1.txt 2.txt 3.txt 
    this is file1
    this is file2
    this is file3
    


    As you can probably guess, the contents of file.txt looks like this:

    1.txt
    2.txt
    3.txt
    
  7. One of the most common uses for xargs is when it’s combined with the find command. find already includes the -exec option so it’s able to execute commands on any files it finds, but xargs is much more efficient. In your find command, you should add the -print0 option so an extra null character is printed after each file name. You’ll also need to include a corresponding -0 option in the xargs command. Here’s an example where we search for .txt files and run cat on each one. Once again, we will append the -t option to see the command xargs ends up running.
    $ find . -name "*.txt" -print0 | xargs -t -0 cat
    cat ./3.txt ./2.txt ./1.txt 
    this is file3
    this is file2
    this is file1
    

These examples should be enough to help you get the most out of the xargs command. There’s a lot you can do with the command and tons of different scenarios where it can come in handy. If you want to learn more, it’s recommended to check out the man page for more usage examples.

$ man xargs

Closing Thoughts

In this guide, we saw how to use the xargs command through various examples on Linux. We combined it with multiple other commands, and also learned how to use it to read the contents of a file. The xargs command has infinite different applications and proves to be one of the most versatile tools on the Linux command line.



Comments and Discussions
Linux Forum