xargs: run a command on a list of items

Updated November 2026

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

OptionMeaning
-0Items are separated by NUL characters (use with find -print0)
-n NAt most N items per command
-I {}Run once per item, replacing {} with it
-P NRun up to N commands at the same time
-rDo not run the command if there is no input (GNU; default on BSD)
-tPrint each command before running it
-pAsk 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?

Often find … -exec command {} + does the same job without xargs and handles every file name safely.