xargs Build commands from standard input
Reads items from standard input and runs another command with them as arguments. Use it when a command takes file names as arguments but you have them as a list, for example the output of find.
Synopsis
xargs [OPTION]... [COMMAND [ARG]...]
find ... -print0 | xargs -0 COMMAND
Common options
| Option or form | What it does |
|---|---|
-0, --null | Expect input items separated by NUL characters instead of whitespace. Pair it with find -print0 so odd file names are safe |
-I replstr | Run the command once per input line, replacing replstr (often {}) with that line wherever it appears in the command |
-n number | Pass at most number arguments to each run of the command. -n 1 runs it once per item |
-P maxprocs | Run up to maxprocs commands at the same time. -P 0 uses as many as possible |
-p | Ask for confirmation before each run |
-t | Print each command to standard error before running it |
-r, --no-run-if-empty | Do not run the command when the input is empty. Needed on GNU/Linux, accepted but not needed on macOS |
-E eofstr | Stop reading input when a line equals eofstr |
Examples
printf 'a.txt\nb.txt\n' | xargs touch
printf '1\n2\n3\n' | xargs -n 1 echo item
item 1
item 2
item 3
find docs -name '*.md' -print0 | xargs -0 wc -l
find photos -name '*.jpg' | xargs -I{} cp {} {}.bak
find docs -type f -print0 | xargs -0 ls -l
find . -name '*.log' -print0 | xargs -0 -P 4 gzip
Notes
- By default xargs splits on spaces, tabs and newlines, and treats quotes and backslashes specially. A file called my notes.txt becomes two arguments. Use find -print0 with xargs -0 for any file names you do not control.
- With empty input, GNU xargs runs the command once with no arguments unless you add -r. macOS (BSD) xargs does not run it at all, and accepts -r only for compatibility. Add -r in scripts that must also run on Linux.
- xargs may split a long list over several runs of the command, because of the system limit on argument length. Results that need all items at once, such as sort, can come out wrong.
- -I{} runs the command once per line and is slower than the default. It also treats each whole line as one item, spaces included.
- Check the list before running something destructive. Put echo in front of the command (xargs echo rm) to preview it.
See also
Last reviewed 2026-10-07.