The xargs command reads items from standard input and runs another command with those items as arguments. Many commands, such as rm, mkdir and cp, take their targets as arguments and ignore standard input, so a pipe alone cannot feed them. find . -name "*.tmp" | xargs rm bridges that gap by turning the list of file names into arguments for rm.
Understanding the difference between input and arguments is the key idea of this guide. The xargs command is part of GNU findutils, which ships with almost every Linux distribution.
A program receives data in two ways: as arguments on its command line, and as a stream on standard input. The cat command reads files named in its arguments, but if it has none it reads standard input. The rm command never reads standard input at all.
The xargs command reads whitespace-separated words from standard input, builds a command line from them and runs the command you gave it. If you give no command, it uses echo.
Create some practice files.
Here xargs ran one ls -l ./one.tmp ./two.tmp command. It packs as many arguments as the system allows into each invocation, which is much faster than starting one process per file.
Plain xargs splits on spaces and newlines and treats quotes and backslashes specially. A file named my report.txt would be split into two arguments, which can delete the wrong files. The safe pattern uses a null byte as the separator, because a file name can never contain one.
Always pair find -print0 with xargs -0 when file names come from find. For simple cases, find can also do the work itself with -exec cmd {} +, which is equally safe and batches arguments in the same way.
By default xargs passes as many arguments as will fit. The -n option limits the count per command run.
The -L 1 option uses one input line per command. The -s option limits the command line size in bytes, and xargs --show-limits prints the system limits. Splitting work like this matters because the kernel limits the total size of arguments, and a huge list passed directly can fail with Argument list too long.
By default the items are appended to the end of the command. With -I, you pick a placeholder and xargs runs the command once per input line, replacing the placeholder.
This runs cp a.txt a.txt.bak and cp b.txt b.txt.bak. The placeholder {} is a convention, but you can use any string. Remember that -I implies one command per line, so it is slower than batching. Lines are split on newlines only, so spaces in names are handled, but names containing newlines are not.
The -P option runs several commands at the same time, which is a quick way to use multiple CPU cores.
Use -P 0 to run as many processes as possible, but be careful: output from parallel processes can interleave, and the order is not guaranteed. Use -n so each process gets a fair share of the work.
| Option | Meaning |
|---|---|
-0 | Items are separated by null bytes |
-n N | At most N arguments per command |
-I {} | Replace {} in the command, one line at a time |
-P N | Run up to N processes in parallel |
-r | Do not run the command if the input is empty (GNU) |
-p | Prompt before each command |
-t | Print each command before running it |
-d '\n' | Use a custom delimiter (GNU) |
Two habits prevent most accidents. First, preview with echo, so find . -name "*.tmp" | xargs echo rm shows the command without running it. Second, add -r (or --no-run-if-empty) so that an empty input does not run the command once with no arguments, for example xargs rm with nothing to delete.
A pipe connects the output of one command to the input stream of the next. The xargs command converts that stream into command-line arguments, which is what programs like rm and mkdir need.
By default it splits on whitespace. Use find ... -print0 | xargs -0 ... so items are separated by null bytes.
They overlap. find -exec cmd {} + is safe and efficient for files that find locates, while xargs also works with any list from any command and supports parallelism with -P.
Use -L 1 or -I {}. Both run the command separately for every input line.
The xargs command turns standard input into command-line arguments, which lets you connect commands like find and grep to tools such as rm, cp and gzip. Use -0 with -print0 for safe file names, -n to batch, -I {} to place the argument, -P for parallel jobs and -r to skip empty input. Preview with echo before running anything destructive.