Skip to content

Instantly share code, notes, and snippets.

@internalsystemerror
Forked from legeyda/foreach
Last active September 19, 2022 16:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save internalsystemerror/814c042f558b903690589e7d3e835760 to your computer and use it in GitHub Desktop.
Save internalsystemerror/814c042f558b903690589e7d3e835760 to your computer and use it in GitHub Desktop.
Bash script to execute a given command in all subdirectories of the current directory
#!/usr/bin/env bash
set -e
trap "exit" INT
USAGE="Usage:
fesd <command>"
# show help if requested
if [ "x$1" == "x-h" ] || [ "x$1" == "x-?" ] || [ "x$1" == "x--help" ]; then
echo "Execute command in each subdirectory of current directory."
echo "$USAGE"
exit 0
fi
# assert arguments supplied
if [ "x$1" == "x" ]; then
echo "No input, 'fesd -h' for help"
echo "$USAGE"
exit 1
fi
# execute commands
for DIR in $(find . -maxdepth 1 -mindepth 1 -type d -not -path '*/.*' | sed "s|^\./||" | sort); do
echo "======== entering $DIR ========"
(cd "$DIR" && bash -c "$*")
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment