Bash

Parallel Map with xargs -P

admin by @admin ADMIN
Jun 18, 2026
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
`xargs -P N` is a Swiss-army knife for parallel map: feed it a list of inputs and a command, it runs N workers. Faster and simpler than bash for-loops with `&`.
Bash
Raw
# Resize 100 images, 8 in parallel
ls *.jpg | xargs -P 8 -I{} convert {} -resize 1024 resized/{}

# More robust: -print0 + -0 for filenames with spaces/newlines
find . -name "*.log" -print0 | xargs -0 -P 4 -I{} gzip {}

# Run a heavy fn over a list — wrap as a function and export it
process() {
    local url="$1"
    curl -fs "$url" -o "downloads/$(basename "$url")"
}
export -f process

cat urls.txt | xargs -P 10 -I{} bash -c 'process "$@"' _ {}

# Limit total work but cap parallelism — GNU parallel is cleaner if installed
seq 1 100 | xargs -P 16 -I{} ./worker.sh {}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.