Created
September 9, 2024 13:09
-
-
Save caruccio/71c512378d895059c53997c4bc3cf8b3 to your computer and use it in GitHub Desktop.
Execute command in a list of directories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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