Skip to content

Instantly share code, notes, and snippets.

@mdelillo
Created August 5, 2020 14:43
Show Gist options
  • Save mdelillo/230ea5d2407724731ff43f45f8fc7794 to your computer and use it in GitHub Desktop.
Save mdelillo/230ea5d2407724731ff43f45f8fc7794 to your computer and use it in GitHub Desktop.
Runs a bash function on all child directories in parallel
#!/usr/bin/env bash
set -euo pipefail
do_something() {
dir=$1
echo "doing something with $dir"
sleep 1
}
main() {
root_dir="$(cd "$(dirname "$0")" && pwd)"
directories_in_parallel="8"
find "${root_dir}" -type d -depth 1 -print0 | \
xargs -0 -P${directories_in_parallel} -I {} bash -c "$(declare -f do_something) ; do_something {}"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment