Skip to content

Instantly share code, notes, and snippets.

@caruccio
Created September 9, 2024 13:09
Show Gist options
  • Save caruccio/71c512378d895059c53997c4bc3cf8b3 to your computer and use it in GitHub Desktop.
Save caruccio/71c512378d895059c53997c4bc3cf8b3 to your computer and use it in GitHub Desktop.
Execute command in a list of directories
# Install: add this function to your ~/.bashrc or ~/.profile
# Open a new shell and use it:
#
# $ for_dir /tmp /home /var -- ls -la
#
function for_dir()
{
local dirs=()
if ! [[ "$*" =~ ' -- ' ]]; then
echo "Usage: $0 DIRS... -- COMMAND"
return 1
fi
while [ $# -gt 0 ]; do
case "$1" in
--)
shift
break
;;
*)
dirs+=( "$1" )
esac
shift
done
for dir in "${dirs[@]}"; do
cd "$dir"
"$@"
cd -
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment