Linux Command Reference ยท Viewing and Searching Files
Linux xargs command
Reference for the Linux xargs command: turn standard input into command arguments, handle spaces with -0, batch with -n, run in parallel with -P and use -I.
xargs reads items from standard input and runs a command with those items as arguments. It bridges the gap between commands that produce lists (such as find, grep -l or ls) and commands that take arguments (such as rm, wc or mv) but ignore standard input.
It also splits very long lists into several runs, so you never hit the operating system's limit on argument length.
xargs [OPTION]... [COMMAND [INITIAL-ARGS]]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
xargs [-0] [-a FILE] [-d DELIM] [-I STR] [-L N] [-n N] [-P N] [-r] [-s SIZE] [-t] [-x] [COMMAND ...]
| Option | Description |
|---|---|
-0, --null |
Items are separated by NUL bytes; quotes and backslashes are not special. |
-d DELIM |
Use a single character as the delimiter, for example -d '\n'. |
-a FILE |
Read items from FILE instead of standard input. |
-n N |
Use at most N items per command run. |
-L N |
Use at most N input lines per command run. |
-I STR |
Replace STR in the command with each item (one run per item). |
-P N |
Run up to N commands at the same time (0 = as many as possible). |
-r, --no-run-if-empty |
Do not run the command when the input is empty. |
-t, --verbose |
Print each command to standard error before running it. |
-p |
Ask before each command. |
-s SIZE |
Maximum command-line length in characters. |
-x |
Exit if the size limit would be exceeded. |
If no command is given, the default is echo.
Returns: 0 if all runs succeeded; 123 if any run exited with 1 to 125; 124 if a run exited with 255; 125 if killed by a signal; 126 if the command could not be run; 127 if it was not found.
Usage notes
- Default splitting is risky. Without
-0, items are split on blanks and newlines, and quotes and backslashes have special meaning. Names with spaces, quotes or newlines break. Pairfind -print0withxargs -0. - GNU
-ris not the default. With empty input, GNUxargsstill runs the command once (for examplermwith no files). Add-rto prevent it; BSD skips it automatically. -Iimplies one item per run and also changes how whitespace is treated (only newlines separate items). It is slower for large inputs.- Parallelism:
-Pis a cheap way to speed up independent jobs. Output from different jobs can interleave, so let each job write its own file. - Replacing
{}more than once is allowed:xargs -I{} cp {} {}.bak. - Shell features need a shell. Redirection and pipes inside the command require
sh -c '...' _, with the items passed as"$@". - Alternatives:
find -exec cmd {} +does the same without a pipe. The shell loopwhile IFS= read -r f; do ... donehandles per-item logic. - Interactive commands (like
vim) lose their terminal. Use-oin GNU xargs 4.7 and later, or</dev/tty.