xargs: run a command on a list of items
xargs reads items from its input and runs a command with them as arguments. It turns a list of file names from find or grep into actions, and can run many jobs in parallel.
command-producing-a-list | xargs [options] command [initial arguments]
Common options
| Option | Meaning |
|---|---|
-0 | Items are separated by NUL characters (use with find -print0) |
-n N | At most N items per command |
-I {} | Run once per item, replacing {} with it |
-P N | Run up to N commands at the same time |
-r | Do not run the command if there is no input (GNU; default on BSD) |
-t | Print each command before running it |
-p | Ask before running each command |
Examples
find . -name "*.tmp" -print0 | xargs -0 rm -f # delete, safe with any file name
find /var/log -name "*.log" -print0 | xargs -0 grep -l "panic" # which logs mention panic
grep -rl "old.example.com" /etc/app | xargs -r sed -i 's/old.example.com/new.example.com/g'
cat hosts.txt | xargs -I {} ssh {} uptime # one SSH per host
find photos -name "*.jpg" -print0 | xargs -0 -n 20 -P 4 jpegoptim --strip-all # 4 parallel jobs
echo one two three | xargs -n 1 echo # one item per command
ls *.csv | xargs -t -I {} cp {} {}.bak # show each command as it runs
File names with spaces
By default xargs splits its input at spaces and newlines, and treats quotes specially. A file called my report.txt becomes two arguments, and the command may delete the wrong file. Always pair find -print0 with xargs -0. With GNU xargs, -d '\n' splits on newlines only.
Why use xargs?
- "Argument list too long":
rm *.logfails when there are hundreds of thousands of files.find . -name "*.log" -print0 | xargs -0 rmsplits the list into batches that fit. - Speed: passing many items per command is much faster than starting a process for each one.
- Parallelism:
-P Nruns up to N commands at once, for example-P "$(nproc)"for one job per CPU core. For more control, look at GNUparallel.
Often find … -exec command {} + does the same job without xargs and handles every file name safely.