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.
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.
Create filenames with spaces and compare argument boundaries
Run the complete block: it prepares every input in a new temporary directory inside a subshell and prints that directory at the end. It does not overwrite files in the current working directory. Inspect the results and remove only the lab data when it is no longer needed.
(
set -eu
labdir=$(mktemp -d)
cd "$labdir"
mkdir reports
printf 'first\nsecond\n' > 'reports/two words.txt'
printf 'third\n' > 'reports/plain.txt'
printf '%s\0' 'reports/two words.txt' 'reports/plain.txt' |
xargs -0 -r -n 1 printf 'ARG=[%s]\n'
find reports -type f -name '*.txt' -print0 | xargs -0 -r wc -l
printf 'two words\n' | xargs -n 1 printf 'SPLIT=[%s]\n'
printf 'Lab files: %s\n' "$labdir"
)
ARG preserves two words.txt as one argument. find traversal can change the per-file output order, but wc should report counts of 2 and 1 with a total of 3. The last example splits two and words into separate arguments. Use NUL separation for arbitrary filenames and recognize -r as GNU xargs empty-input handling. Inspect argument boundaries with printf before attaching a destructive command.
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.




