Home·Terminal commands

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

FlagMeaning
-nHow many arguments to hand over per run
-IPut the argument where a placeholder such as {} sits
-0Read NUL-separated input, to pair with find -print0
-PRun that many copies at once
-rDo not run the command at all on empty input
-tPrint each command before running it

Examples

find . -name '*.log' -print0 | xargs -0 rm

Deletes them even when names hold spaces.

xargs -n1 -P4 curl -O < urls.txt

Four 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