Quick take: The xargs command turns input into arguments for another command. find . -name '*.log' | xargs rm deletes every match. Use -I {} to place each item precisely, and -0 with find -print0 for filenames with spaces.
Introduction
The xargs command reads items from standard input and uses them to build and run command lines. It bridges commands that produce lists (like find or grep) with commands that take arguments (like rm, cp, or grep), and it does so efficiently by batching many items into few invocations.
This guide covers basic usage, placing arguments with -I, handling filenames safely, and the find + xargs pattern that powers countless batch operations.
Syntax
The basic syntax of the xargs command is:
command | xargs [OPTIONS] [COMMAND]Common Options and Parameters
The most useful options and parameters for the xargs command:
| Option | Description |
|---|---|
| -I {} | Replace {} with each input item (one command per item). |
| -n N | Use at most N arguments per command invocation. |
| -0 | Read NUL-separated input (pair with find -print0). |
| -p | Prompt before running each command. |
| -r | Do not run the command if input is empty. |
| -P N | Run up to N commands in parallel. |
Practical Examples
Real xargs commands you can run today:
# Delete files found by find
find . -name '*.tmp' | xargs rm
# Safely handle spaces in filenames
find . -name '*.log' -print0 | xargs -0 rm
# Run one command per item
cat hosts.txt | xargs -I {} ssh {} uptime
# Limit arguments per call
echo {1..100} | xargs -n 10 echo
# Grep through files found by find
find . -name '*.py' | xargs grep 'TODO'
# Run 4 jobs in parallel
cat urls.txt | xargs -P 4 -I {} curl -O {}Tips and Best Practices
- Always pair
find -print0withxargs -0so filenames containing spaces or newlines are handled safely. - Use
-I {}when the argument is not at the end of the command, e.g.xargs -I {} mv {} /backup/. - Add
-P Nto parallelise slow per-item work like downloads or remote commands.
Final Thoughts
xargs is the glue between list-producing and argument-taking commands, turning output into action efficiently and safely. Learn -I {} for precise placement, -0 for safe filenames, and -P for parallelism, and the find + xargs combination becomes one of the most powerful batch-processing tools in Linux.
FAQ: xargs Command in Linux
What does xargs do in Linux?+
xargs reads items from standard input and turns them into arguments for another command. For example, find . -name '*.log' | xargs rm passes every matching file to rm to delete them.
How do I handle filenames with spaces in xargs?+
Use NUL separation: find . -print0 | xargs -0 command. The -print0 and -0 pair splits on null bytes instead of whitespace, so spaces and newlines in names are safe.
How do I run a command once per item with xargs?+
Use -I with a placeholder: xargs -I {} cp {} /backup/ runs the command separately for each input item, substituting {} with the item.
What is the difference between xargs and find -exec?+
Both run commands on found files. find -exec is built in and simple, while xargs is faster for large lists because it batches many arguments into fewer command runs and supports parallelism with -P.
How do I run xargs jobs in parallel?+
Use -P with the number of parallel jobs: xargs -P 4 -I {} runs up to four commands at once, which speeds up slow per-item tasks like downloads.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support