How to Use xargs to Build Commands From Input in Linux
xargs takes input from standard input (often piped from another command) and uses it to build and execute a new command.
Published October 2, 2026
xargs reads items from standard input and runs a command with them as arguments, bridging commands that output lists with commands that need arguments.
How it works
Many tools do not read stdin; xargs turns a list into arguments. -n limits items per call, -I {} substitutes each item and -0 handles null-delimited input.
Use find -print0 | xargs -0 to be safe with spaces in filenames.
Try it
ls *.log | xargs rm
find . -name '*.tmp' -print0 | xargs -0 rm
Practical examples
Delete found files safely
find . -name '*.tmp' -print0 | xargs -0 rmRun in parallel
cat urls.txt | xargs -n1 -P4 curl -sOPlaceholder
ls *.jpg | xargs -I {} convert {} {}.pngCount lines in many files
find src -name '*.js' | xargs wc -lCommon mistakes
- Breaking on filenames with spaces (use -0).
- Running with empty input (use -r).
- Building command lines that exceed limits unknowingly.
Key takeaways
- xargs turns stdin into arguments.
- Use -0 with find -print0.
- -P runs in parallel.
FAQ
xargs vs find -exec?
Similar; xargs batches arguments and can parallelise.
What is -r?
Do not run if the input is empty (GNU).