Run commands in parallel with xargs
Bash
#!/usr/bin/env bash
# Compress many log files in parallel with null-safe xargs.
set -euo pipefail
dir="${1:?usage: $0 DIR}"
jobs="${JOBS:-4}"
if [[ ! -d $dir ]]; then
printf 'not a directory: %s\n' "$dir" >&2
exit 1
fi
command -v gzip >/dev/null || { printf 'gzip is required\n' >&2; exit 1; }
# -print0 / -0 keep file names with spaces and newlines intact.
# xargs exits non-zero when any child fails, so set -e stops the script.
find "$dir" -type f -name '*.log' -print0 |
xargs -0 -r -n 1 -P "$jobs" gzip -9 --
printf 'compressed .log files in %s using %s workers\n' "$dir" "$jobs"