xargs run a command for each input line

xargs reads items from standard input and runs a command with those items as arguments. It connects tools that print lists (find, grep -l, sqx -o raw) to tools that take arguments, and can run several commands in parallel.

Availability Preinstalled on macOS, Linux and BSD

Syntax

xargs [options] command [initial-arguments]

Common options

OptionWhat it does
-n NUse at most N items per command
-I {}Replace {} in the command with each item, running once per item
-P NRun up to N commands at the same time
-0Items are separated by NUL characters (pair with find -print0)
-tPrint each command before running it

Examples

Delete files found by find, safely

find . -name '*.tmp' -print0 | xargs -0 rm

Fetch headers for each URL, 4 at a time

xargs -n 1 -P 4 curl -sI < urls.txt

Use xargs with sqx

Run a command for every result row

-o raw prints bare values, one per line, ready for xargs.

sqx -f users=users.csv -o raw "SELECT email FROM users WHERE plan = 'enterprise'" | xargs -n 1 echo notify:

Import every CSV file into one database

sqx import appends to the table and adds new columns when files differ.

find exports -name '*.csv' | xargs -I {} sqx import -db exports.db -t rows {}

File names with spaces break plain xargs. Use find -print0 with xargs -0, or -I {} with one item per line.

Full documentation

This page covers the essentials. For every option, see the official documentation:

Or read the manual in your terminal: man xargs