Skip to content

Instantly share code, notes, and snippets.

@okovalov
Last active September 9, 2016 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okovalov/e26a4fb282564a6da6f50593abf05526 to your computer and use it in GitHub Desktop.
Save okovalov/e26a4fb282564a6da6f50593abf05526 to your computer and use it in GitHub Desktop.
Hi guys. Recently I had a situation when a certain directory had a few more sub-directories, and
I needed to perform an identical list of actions for each of those sub-directories and doing that
manually by changing the current directory and repeatedly executing same commands was quite annoying
so I decided to spend half an hour and create a bash script for that purpose.
A particular example of the situation I had was having several virtual machines located accordingly
in ~/Boxes/serverOne ~/Boxes/serverTwo ~/Boxes/serverThree (I was testing ansible and was recreating and destroying vms).
This script asks for the confirmation as well.
You can find a script here https://gist.github.com/okovalov/8f3be1e1e68c47d18cb34992527492b1
Example of how to use it
exec_command_for_dirs ~/Boxes *Provision* 'vagrant halt; vagrant destroy'
Here exec_command_for_dirs is ~/.bash_profile function which uses an executable
file ~/bin_scripts/exec_command_for_dirs.sh but that is just a more convenient way I prefer.
You obviously could call that script directly
~/bin_scripts/exec_command_for_dirs.sh ~/Boxes *Provision* 'vagrant halt; vagrant destroy'
Also here is my ~/.bashrc function to run it
exec_command_for_dirs () { ~/bin_scripts/exec_command_for_dirs.sh "$@" "$@" "$@"; }
Not a perfect one but it works. I hope it will help someone as it helped me.
@okovalov
Copy link
Author

okovalov commented Sep 9, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment