Skip to content

Instantly share code, notes, and snippets.

@stevobengtson
Created May 15, 2014 22:16
Show Gist options
  • Save stevobengtson/22888f6d8310ad7acfbc to your computer and use it in GitHub Desktop.
Save stevobengtson/22888f6d8310ad7acfbc to your computer and use it in GitHub Desktop.
Brew clean up script
#!/bin/bash
#
# Clear out old, inactive versions of software
# installed by Homebrew.
# Created by Colin and listed on http://www.nonsenseby.me/blog/2014/01/12/remove-old-versions-in-homebrew/
#
set -o noglob
# Some brew settings.
readonly BREW_CMD=$( which brew )
readonly BREW_AWK="/tmp/brew.$$.awk"
# Create temporary awk script.
cat > $BREW_AWK <<EOF
BEGIN {sw=0; used="";}
\$0 ~ cellar {
n=split(\$1,r,"/");
if (\$NF == "\*") {
used=r[n];
}
else {
vers[sw++] = r[n];
}
}
END {
if (used != "" && sw>0) {
for (idx in vers) {
system(brew " switch " tap " " vers[idx]);
system(brew " remove " tap);
}
system(brew " switch " tap " " used);
}
}
EOF
# Get a list of installed things
readonly INSTALLED_FORMS=$( ${BREW_CMD} list )
for tap in $INSTALLED_FORMS
do
# Cellar location for this tap
# ex: /usr/local/Cellar/PROGRAM
cellar=$( ${BREW_CMD} --cellar ${tap} )
# Escape all the slashes: / -> \/
awk_cellar=$( ${BREW_CMD} --cellar ${tap} | sed -e 's/\//\\\//g')
# Pass the tap info to the AWK script.
${BREW_CMD} info ${tap} | awk -v brew="$BREW_CMD" -v cellar="$awk_cellar" -v tap="$tap" -f $BREW_AWK
done
# Clean-up temporary script.
rm $BREW_AWK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment