Skip to content

Instantly share code, notes, and snippets.

@lovromazgon
Created June 11, 2024 11:56
Show Gist options
  • Save lovromazgon/6f65188b34f5625a6ea50af05b6b3b98 to your computer and use it in GitHub Desktop.
Save lovromazgon/6f65188b34f5625a6ea50af05b6b3b98 to your computer and use it in GitHub Desktop.
Execute the same command in multiple folders
#!/bin/bash
# multicmd.sh is a simple utility to execute the same command in multiple folders.
# It's useful for managing multiple git repositories at the same time.
#
# The commands are executed sequentially in each folder. If a command fails, the
# execution will pause and ask if you want to continue the execution or stop entirely.
#
# IMPORTANT: Before running the script, update the paths array to include all
# folders where the command should be executed.
#
# Usage examples:
# ./multicmd.sh git status
# ./multicmd.sh 'git commit -m "my commit message"'
dir=`pwd`
declare -a paths=(
# ------------ UPDATE PATHS HERE ------------
"relative/path/to/folder"
"/absolute/path/to/folder"
# -------------------------------------------
)
function error ()
{
echo "$1"
while true; do
read -p "Do you want to proceed? (y/n) " yn
case $yn in
y ) break;;
n ) echo exiting...;
exit 1;;
* ) echo invalid response;;
esac
done
}
for path in "${paths[@]}"
do
cd "$path"
eval "$@" || error "$path"
cd "$dir"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment