Text and processing
The xargs command
Turns piped input into arguments for another command.
Most useful options
-0, --nullSeparates items with a null byte instead of spaces or newlines. Use it with find -print0 when names may contain spaces-I textoRuns the command once per item and replaces that text with the item. It is often used with {}, which is only a convention-n NUses at most N arguments per run. With -n 1, the command runs once per item-P NRuns up to N processes at once. With 0, it uses as many as it can-r, --no-run-if-emptyDoes not run the command if the input is empty. Without this option, it runs once with no argumentsCopy-ready examples
$ printf 'a b c\n' | xargs echopasses the words of the input as arguments to echo.Example output
a b c
$ find . -name '*.log' -print0 | xargs -0 ls -llists the .log files found, even when their names contain spaces.$ printf '%s\n' uno dos | xargs -I {} echo 'Procesando {}'runs a command for each line and places the item where {} is.Example output
Procesando uno Procesando dos
$ ls *.txt | xargs -n 1 wc -lcounts the lines of each text file, one at a time.$ find . -name '*.tmp' -print0 | xargs -0 -r rm -fdeletes the .tmp files in the current folder and its subfolders. It is destructive: run the previous example first and confirm the list is the right one.You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option | The input has an unclosed quote, and xargs treats quotes as special by default. | Use find -print0 with xargs -0, so quotes are treated as plain text. |
| cat: x: No such file or directory | xargs passed cat a name that does not exist. The input had a misspelled name or leftover text from another command. | Run the previous command without the pipe first, check the list and fix the name. |
Typical mistakes
- Without -0, spaces, quotes and newlines in the input change the result. Use find -print0 with xargs -0.
- With empty input, xargs still runs the command once. Use -r to avoid it.
- xargs does not ask for confirmation. Before rm or mv, put echo in front of the command and check what would run.
Differences from similar commands
for: loops over the items inside the shell itself, one command per item. xargs gathers the arguments and starts fewer processes, which helps with many files.find -exec: runs the command from find, without a pipe. xargs does the same with the input that arrives through a pipe.
FAQ
Why does xargs run the command even with no input?
Because by default it runs it once with no arguments. Add -r so it does nothing when the input is empty.
How do I put the argument in the middle of the line?
With -I text: the command runs once per item and the text is replaced wherever it appears. For example, xargs -I {} cp {} copies/.
What do I do if xargs complains about a quote?
An apostrophe or quotes inside the names break the parsing. Use find -print0 and xargs -0, which do not interpret quotes.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course