Uses of the "xargs" Command

Use 'xargs' when a standard pipe or redirection symbol won't work

The xargs command is typically used in a command line where the output of one command is passed on as input arguments to another command.

In many cases, no special command such as xargs is needed to accomplish that purpose, because the pipe and redirection operators perform the same type transaction. However, sometimes you'll experience glitches with the basic piping and redirection mechanism—e.g., if arguments contain spaces.

Additionally, xargs executes the specified command repeatedly, if necessary, to process all the arguments given to it. In fact, you can specify how many arguments should be read off the standard input stream each time xargs executes the specified command.

When to Use It

In general, use the xargs command if the output of one command serves as part of the options or arguments of a second command to which the data are streamed (using the pipe operator "|"). Regular piping is sufficient if the data are intended to serve as the standard input of the second command.

For example, use the ls command to generate a list of file names and directories, then pipe this list into the xargs command executing wc. You'll obtain the word counts of each of the specified files:

ls *.txt | xargs wc
xargs example

If you execute a command that returns a large and unpredictable number items (e.g. file names) which are passed on to another command for further processing, it is a good idea to control the maximum number of arguments that second command receives to avoid overload and crashing.