xargs
Text processing
Usage
[cmd] | xargs [cmd2]Turns lines of input into arguments for another command; names with spaces break it unless both sides agree on NUL, meaning find -print0 with xargs -0.
Common flags
| Flag | Meaning |
|---|---|
| -n | How many arguments to hand over per run |
| -I | Put the argument where a placeholder such as {} sits |
| -0 | Read NUL-separated input, to pair with find -print0 |
| -P | Run that many copies at once |
| -r | Do not run the command at all on empty input |
| -t | Print each command before running it |
Examples
find . -name '*.log' -print0 | xargs -0 rmDeletes them even when names hold spaces.
xargs -n1 -P4 curl -O < urls.txtFour downloads at a time.
ls *.txt | xargs -I{} mv {} old/{}Uses {} where the file name goes.
These were built to be piped together. Three joined commands are usually shorter than one that does everything.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does xargs do?
Turns lines of input into arguments for another command; names with spaces break it unless both sides agree on NUL, meaning find -print0 with xargs -0.
Q. How do I type it?
[cmd] | xargs [cmd2] — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
6 are listed here; the full set is in man xargs. This command sits under Text processing.
Related commands
man page: man xargs