Viewing and processing text

Command xargs

Pass a list of arguments to another command

Linux · Bash examples · commands run on the selected server

When to use xargs

Use xargs to build bounded command invocations from an input stream. Filenames can contain spaces and newlines, so use a delimiter-safe producer and consumer together before performing file operations.

Syntax

xargs [OPTIONS] [COMMAND [ARGUMENTS...]]

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-0
Separate input arguments with a null byte.
-n N
Limit the number of arguments per run.
-r
In GNU xargs, do not run the command on empty input.

Usage examples

Show names of found files

find ./logs -type f -print0 | xargs -0 -r printf '%s\n'

Null-delimited paths are printed without splitting on spaces or newlines; empty input does not launch a command.

Pass two arguments

printf '%s\n' one two three | xargs -n 2 printf '%s\n'

Input arguments are processed in groups of at most two.

Count lines in files

find ./src -type f -name '*.txt' -print0 | xargs -0 -r wc -l

Matching text files are passed safely to the line counter in batches.

Notes and limitations

  • Normal parsing of xargs respects spaces and quotes; for filenames use the pair -print0 and -0.
  • Explicitly limit parallel execution via -P, especially for network and resource-intensive operations.

Common questions and troubleshooting

Why do filenames with spaces break my pipeline?

Default whitespace splitting does not preserve arbitrary filenames. Pair a null-delimited producer with xargs null-input mode.

Does xargs run everything in parallel?

Not by default. Parallel execution requires an explicit setting and careful consideration of output ordering, resource limits and conflicting writes.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man xargs.