Linux xargs Command: Pass Standard Input as Command Arguments
xargs reads items from standard input and passes them as arguments to other commands. It is used when the output of a pipe should be treated as arguments like filenames rather than input data to a command.
What is the xargs command?
The default delimiters are spaces and newlines, and quotes and backslashes are also interpreted. Therefore, when processing arbitrary filenames, pair find -print0 with xargs -0 to safely separate them with a NUL character.
Basic syntax
Generate command | xargs [options] execute command [fixed arguments...]
The installed implementation and options may vary depending on the distribution. Check the description on your current system with man xargs.
Examples
Passing values as arguments
Groups input lines one or more at a time and passes them to a command.
printf '%s\n' one two three | xargs printf '%s\n'
Safely passing filenames
Preserves filenames with spaces or newlines by using NUL separation.
find reports -type f -name '*.txt' -print0 | xargs -0 -r wc -l
Execute one at a time
If separate execution for each item is required, use -n 1.
printf '%s\n' alpha beta | xargs -n 1 printf 'item: %s\n'
The process of changing standard input to arguments
xargs passes input items as arguments to the command specified after it. -n 1 runs the command once for each item.
printf 'one\ntwo\n' | xargs -n 1 printf 'item=%s\n'
Example output:
item=one item=two
By default, xargs interprets spaces, quotes, and backslashes as delimiters, so it is risky to use with a list of filenames containing spaces or newlines. When passing filenames, use the NUL-separated output of the generating command together with xargs -0. Before connecting it to commands that make changes, such as deletion, it is safer to first check the arguments to be passed with printf.
Do not treat newline-separated text as a safe filename list when names can contain spaces or shell metacharacters. Match the delimiters at both ends of a pipeline, for example with find ... -print0 | xargs -0 .... GNU xargs -r avoids running the command when there is no input.
Main Options and Format
| Options/Format | Description |
|---|---|
-0 |
Separate items with the NUL character. |
-r |
In the GNU implementation, it does not execute if the input is empty. |
-n N |
Pass up to N arguments per command execution. |
-P N |
Execute up to N commands in parallel. |
-I placeholder |
Substitute input items at the specified position in the command. |
Precautions when using
If you pass file names based on line breaks, like find ... -print | xargs rm, names containing spaces or newlines will be incorrectly split. For deletion tasks, use NUL separation or find -exec and check the targets first. Parallel execution with -P changes output order and resource contention.
Frequently Asked Questions
How is using only a pipe different from xargs?
command1 | command2 sends it to the standard input of command2. xargs constructs the values read as arguments to command2.
Official Documentation
You can check the exact behavior of options and implementation differences on the xargs manual page.









