Fotolia_65716003_XS.jpg

What’s the problem?

I like to keep my system up to date, and I depend on many Homebrew packages on my OS X machine. Homebrew allows for this but I:

  • want it automated
  • want it to delete old versions that I am no longer using

How did I solve it?

First, I added this into my .zshrc (could be in your .profile if you use Bash):

1
2
3
4
5
6
7
8
9
10
11
12
13
function brew_update() {
brew update
brew upgrade --all
ruby $HOME/remove_old_brews.rb
}
CURR_DATE=`date '+%Y-%m-%d'`
LAST_DATE=`head -n 1 $HOME/.brew_update_date 2>/dev/null`
if [ "$CURR_DATE" != "$LAST_DATE" ]; then
echo $CURR_DATE > $HOME/.brew_update_date
echo Updating Homebrew...
brew_update
fi

Then, I created remove_old_brews.rb in my home directory:

Wait, what?!

Ok, first the shell will check the current date and compare it with a date stored in a .brew_update_date file. If they differ, it immediately updates the file (so I can open another Terminal while the update is in progress), then it calls brew_update.

brew_update tells Homebrew to update its index, then upgrade all out of date packages. Finally, brew_update calls remove_old_brews.rb

remove_old_brews.rb looks at all installed packages in /usr/local/Library/LinkedKegs and finds packages with more than 1 version installed. For those, it finds whichever ones are not the currently linked version and removes them.

Things to consider

This is tuned for my workflow, but yours may differ. What I wanted was:

  • update the first time I open Terminal in a day
    • meaning if I really need a Terminal, I can just open another and the 2nd one won’t update
  • keep what was updated at the front of my attention (which is why I didn’t just put it into cron)

And that’s about it.

Comment and share

  • page 1 of 1

Craig St. Jean

Father, programmer, constant learner, @pluralsight author


Software Architect