xargs is a unix command used to build and execute commands from standard input.

Some commands can accept standard input as a parameter or argument by using pipe (for example grep uses pipe). Other commands disregard the standard input stream and rely solely on arguments after the command (such as echo).

Quick Links:

Example 1 – echo

The following example shows
echo 1 2 3 4

1 2 3 4

echo 1 2 3 4 | xargs

1 2 3 4

echo 1 2 3 4 | xargs -n 2

1 2
3 4

echo 1 2 3 4 5 6 7 8 | xargs -n 1




Example 2a – Combining Find and xargs

This example will show how to combine the xargs command with the find command. This combination is one of the most important usage of the xargs command. It allows you to find certain types of files and perform certain actions on these files (most common being delete)

First step will be to create example files which we can then manipulate. Run the following command:

touch one.a one.b two.a two.b

If you execute the ls command you should see:

one.a  one.b  two.a  two.b

Now we can run the find command along with xargs and rm:

find . -name “*.b”

it should return:

Now run the following command:

find . -name “*.b” | xargs rm -rf

This will delete the two files found with the previous command.

find . -name “*.b” should now return nothing. To confirm the files have been deleted you can execute the ls command again:

one.a two.a


Example 2b – find and xargs (removing files with white-space in file name)

The following example will show how the command in example 2a will not remove files with white-space in the name (e.g file name: white spacefile.c)

touch “white spacefile.c”


white spacefile.c

find . -name “*.c” | xargs rm -rf


white spacefile.c

If you execute the following command it will delete files with white-space in the file name.

find . -name “*.c” -print0 | xargs -0 rm -rf

Now if you run the ls command the file will be deleted.