Nov 19, 2022 5 min read

How to Use the Powerful Xargs Command in Linux [Explained with Examples]

Table of Contents

xargs is one of the most powerful commands in Linux. In this tutorial, I’ll show you how you can xargs to convert the standard input into commands.

What is xargs command?

The xargs command reads lines of text from the standard input or from the output of another command and turns them into commands and execute them.

You’ll often find xargs command being used with the find command. The find command gives you a list of filenames and the xargs command lets you use those filenames, one by one, as if it was input to the other command.

Since xargs works on redirection, I highly recommend brushing up your knowledge of stdin, stdout and pipe redirection in Linux.

How to use xargs command?

xargs command examples in linux

xargs command has the following syntax:

xargs [options] [command [initial-arguments]]

But you are probably not going to use it like that. It’s power lies in combining the output of one command to another. Let’s see it with a simple example.

In my current directory, I have some text files and the flowers.txt has the name of all these files:

[email protected]:~/tutorial$ ls
flowers.txt  lily.txt  one_lotus.txt  rose.txt  three_lotus.txt  two_lotus.txt
[email protected]:~/tutorial$ cat flowers.txt 

Now my aim is that I want to see the file size of all the files mentioned in the flowers.txt. Common sense says that I could combine cat command to display all the filenames and then pipe it to the du command to check the file size.

But if I pipe it directly, it won’t give the size of each of the files mentioned in the flowers.txt file.

[email protected]:~/tutorial$ du -h
52K	.
[email protected]:~/tutorial$ cat flowers.txt | du -h
52K	.

Why? First, the du command doesn’t take standard input. Second, the output of the cat command is not individual file names. It’s a like a words separate by new line character.

The magic of xargs command is that it will take this and the text separated by blanks or new lines and convert them into individual inputs to the next command.

[email protected]:~/tutorial$ cat flowers.txt | xargs du -h
4.0K	lily.txt
4.0K	one_lotus.txt
16K	rose.txt
4.0K	three_lotus.txt
16K	two_lotus.txt

Think of it as equivalent to feeding those filenames to the du command:

[email protected]:~/tutorial$ du -h lily.txt one_lotus.txt rose.txt three_lotus.txt two_lotus.txt 
4.0K	lily.txt
4.0K	one_lotus.txt
16K	rose.txt
4.0K	three_lotus.txt
16K	two_lotus.txt

You realize the power of xargs command now, don’t you?

xargs and find: made for each other

You’ll often find it being used in combination of the “find command”.

The find command searches for files and directories and returns their names. Thanks to xargs, you can use the result of the find command for specific purposes like renaming them, moving them, deleting them and whatnot.

Let’s say, you want to get all the files ending in .txt and containing the word red. You can combine find and grep commands with the help of xargs:

[email protected]:~/tutorial$ find . -type f -name "*.txt" | xargs grep -l red

The find exec command combination works similarly. But let's focus on the xargs command here.

Dealing with file names with spaces

If you have file with space in its name, it will cause issues. Let’s say I renamed three_lotus.txt to “three lotus.txt”. Now when it is processed via xargs, it is seen as two separate files as three and lotus.txt.

[email protected]:~/tutorial$ find . -type f -name "*.txt" | xargs grep -l red
grep: ./three: No such file or directory
grep: lotus.txt: No such file or directory

In such cases, you should use the -print0 option of the find command. It separates lines with ASCII null characters instead of newline characters. Similarly, you should also use xargs with -0 to accept the ASCII nulls.

[email protected]:~/tutorial$ find . -type f -print0 -name "*.txt" | xargs -0 grep -l red
./three lotus.txt

See what command is being executed

If you want to see what command is being executed with the help of xargs, you can use the -t option. It will print the actual command being executed.

[email protected]:~/tutorial$ find . -type f -name "*.txt" | xargs -t touch
touch ./three_lotus.txt ./two_lotus.txt ./lily.txt ./rose.txt

Force xargs to prompt for confirmation before running the command

Some situations demand to be extra careful like deleting files. It will be a good idea to see what command is going to be executed and have the option to deny the execution.

You can use the -p option of xargs to get the prompt.

[email protected]:~/tutorial$ find . -type f -name "*.txt" | xargs -p rm
rm ./three_lotus.txt ./two_lotus.txt ./lily.txt ./rose.txt ?...n

Using placeholder with xargs to get more control over it

By default, the xargs command adds the standard input as argument at the end of the command. This creates a problem when you need to use that before the last argument.

For example, if you use the move command, you need the source first and then the target. If you want to move the found files to a target directory, this command won’t work:

[email protected]:~/tutorial$ find . -type f -name "*.txt" | xargs -p mv new_dir
mv new_dir ./three_lotus.txt ./two_lotus.txt ./lily.txt ./rose.txt ?...y
mv: target './rose.txt' is not a directory

This is where you can use placeholders in xargs with option -I like this:

[email protected]:~/tutorial$ find . -type f -name "*.txt" | xargs -p -I {} mv {} new_dir
mv ./three_lotus.txt new_dir ?...n
mv ./two_lotus.txt new_dir ?...n
mv ./lily.txt new_dir ?...n
mv ./rose.txt new_dir ?...n

Think of it as if xargs gets all the file names from the find command and keep it in {}. Then it goes to the mv command and supplies the content of {}.

The major difference here is that instead of putting all the file names in the same command, it adds them one by one. This is why the mv command’s been called for each argument (as you can see in the above example).

Note: I used {} as placeholder. You could most other letters or characters as placeholder. {} is safe bet and easy to understand and distinguish.

Running multiple commands with xargs

You may use the placeholders to run multiple commands with xargs.

[email protected]:~/tutorial$ find . -type f -name "*.txt" | xargs -I {} sh -c 'ls -l {}; du -h {}' 
-rw-rw-r-- 1 abhishek abhishek 0 May 28 17:02 ./three_lotus.txt
0	./three_lotus.txt
-rw-rw-r-- 1 abhishek abhishek 0 May 28 17:02 ./two_lotus.txt
0	./two_lotus.txt
-rw-rw-r-- 1 abhishek abhishek 0 May 28 17:02 ./lily.txt
0	./lily.txt
-rw-rw-r-- 1 abhishek abhishek 0 May 28 17:02 ./rose.txt
0	./rose.txt

Keep in mind that placeholder won’t extend to the next pipe redirection or other command. This is why I used sh command here.

There’s always more…

I have mainly used find with xargs command in the examples here because that’s what you’ll see the most. But that doesn’t mean xargs is restricted to be used with find command only. One such practical example of xargs command is when you want to stop all running docker containers:

docker ps -q | xargs docker stop

Like most other Linux commands, there are plenty more options with xargs as well. You can always refer to the man page of xargs command for more information.

I think the xargs command examples listed here are enough to give you a good understanding of this awesome command.

If you have any questions or suggestions, please let me know in the comment section.

Abhishek Prakash
Creator of Linux Handbook and It's FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries from Agatha Christie and Sherlock Holmes to Columbo & Ellery Queen.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.