How to Use the xargs Command in Linux

Xargs Feature

For some of the more experienced users, we’re always looking to find new ways to work smarter and not harder at the terminal. xargs is a useful command that acts as a bridge between two commands, reading output of one and executing the other with the items read. The command is most commonly used in scenarios when a user is searching for a pattern, removing and renaming files, and more. Here we show you how to use the xargs command to your advantage.

What Is xargs?

In its basic form, xargs reads information from the standard input (or STDIN) and executes a command one or more times with the items read. There are many simple demonstrations, but here’s one that shows what I mean.

To have xargs execute the ls command on my Documents folder, I’d run the following command:

echo "Documents" | xargs ls
Xargs Ls Documents

The pipe character | is piping whatever comes before that as STDIN for xargs.

You can see that xargs reads my Documents folder with no problem. This is just one example of the capabilities of the xargs command.

While the xargs command can be used in various command line operations, it comes in really handy when used with the find command. In this article, we discuss some useful examples to understand how xargs and find can be used together.

Operations Involving Multiple Files

Suppose you want to copy the contents of “ref.txt” to all text files present in a directory. While the task may otherwise require you to execute multiple commands, the xargs command, along with the find command, makes it simple. I have a couple of test directories. One has “test0.txt”, which contains text, and the other directory has 10 other test files with no text in them. If I wanted to take the content of test0.txt and copy that to the rest of the text files in the other directories, I’d run the following command:

find ./test-dir1/ -name "*.txt" | xargs -n1 cp test0.txt
Xargs Find Cp

To understand the command shown above, let’s divide it into two parts.

The first part is find ./test-dir1/ -name "*.txt" , which searches for all the .txt files present in the “test-dir1” directory. You can specify any directory here.

The second part, xargs -n1 cp test.txt, will grab the output of the first command (the resulting file names) and hand it over to the cp (copy) command one by one. Note that the -n option is crucial here, as it instructs xargs to use one argument per execution.

When combined together, the full command will copy the content of “test0.txt” to all .txt files in the directory.

Xargs Cp Proof

Operations Involving Large Number of Arguments

One of the major advantages of using xargs is its ability to handle a large number of arguments. For example, while deleting a large number of files in one go, the rm command would sometimes fail with an “Argument list too long” error. That’s because it couldn’t simply handle such a long list of arguments. This is usually the case when you have too many files in the folder you want to delete.

Let’s say you have 75 PDFs and you’re getting an error trying to delete them.

Xargs Pdfs

This can be easily fixed with xargs. To delete all these files, use the following command:

find ./test-dir2/ -type f -name "*.pdf" -print | xargs rm -f
Xargs Rm F

Operations Involving Pattern Search

Software developers as well as system administrators do a lot of pattern searching while working on the command line. For example, a developer may want to take a quick look at the project files that modify a particular variable, or a system administrator may want to see the files that use a particular system configuration parameter. In these scenarios, xargs, along with find and grep, makes things easy for you.

For example, to search for all “.txt” files that contain the “maketecheasier” string, run the following command:

find ./ -name "*.txt" | xargs grep "maketecheasier"

Here is the output the command produced on my system.

Xargs Grep Mte

Cut/Copy Operations

Xargs, along with the find command, can also be used to copy or move a set of files from one directory to another. For example, to move all the text files that are more than 10 minutes old from the current directory to the previous directory, use the following command:

find . -name "*.txt" -mmin +10 | xargs -n1 -I '{}' mv '{}' ../

The -I command line option is used by the xargs command to define a replace-string which gets replaced with names read from the output of the find command. Here the replace-string is {}, but it could be anything. For example, you can use “file” as a replace-string.

find . -name "*.txt" -mmin 10 | xargs -n1 -I 'file' mv 'file' ./practice

How to Tell xargs When to Quit

Suppose you want to list the details of all the .txt files present in the current directory. As already explained, it can be easily done using the following command:

find . -name "*.txt" | xargs ls -l

But there is one problem: the xargs command will execute the ls command even if the find command fails to find any .txt file. The following is an example.

Xargs Find Txt Pngs

So you can see that there are no .txt files in the directory, but that didn’t stop xargs from executing the ls command. To change this behavior, use the -r command line option:

find . -name "*.txt" | xargs -r ls -l

Conclusion

Although I’ve concentrated here on using xargs with find, it can also be used with many other commands. If you have multiple, complex commands that you need to execute, xargs is a very useful tool.

If you enjoyed our writeup on how to use the xargs command in Linux, make sure to check out some of our other Linux content, like our guides on fixing the “no space left on device” error or speeding up Ubuntu.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox