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 rm

Run in parallel

cat urls.txt | xargs -n1 -P4 curl -sO

Placeholder

ls *.jpg | xargs -I {} convert {} {}.png

Count lines in many files

find src -name '*.js' | xargs wc -l

Common 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).

More Linux articles