HOWTO: Clean the distribution files for all installed MacPorts packages

Mon, 03 Sep 2007 12:41:00 -0700
mac, apple, macports, howto, clean, distfiles

I've been using a MacBook Pro for a while now. I got it when I started getting deep into the Ruby/Rails stuff. Being a developer, I use a lot of unix-y command-line tools, server software, and so on. The best way to install these types of things is to use some sort of package manager, such as Fink or MacPorts, the latter being the one I've chosen to use. With MacPorts, I can install, upgrade, or uninstall most of the open source apps that I love in Linux. However, there's a catch to MacPorts' ease of use: it doesn't clean up the packages that it downloads when you install/upgrade/uninstall an app unless you tell it to.

An example of an app that I installed recently that would be a pain to compile/install using a traditional build script is Tomcat 5. Tomcat 5 has 20+ library dependencies, some of which have their dependencies of their own. When I ran sudo port install tomcat5, it took about 20 minutes or so to download and compile everything. However, I never had to do anything else. I literally walked away and checked back periodically until it was done.

Today I decided that, since I was only using Tomcat for occasional development, I'd rather have it, and its plethora of dependencies, installed on a Linux VM instead of my real machine, so it didn't sit there eating up resources all the time. So, I went through and uninstalled everything. But I noticed a while ago that there was a 'clean' command for MacPorts that removed the downloaded packages from an install of an app, and I was curious about whether or not my uninstalling of Tomcat and it's dependencies resulted in these packages being auto-cleaned. So, I took a look in /opt/local/var/macports/distfiles and was surprised to find that the install packages for most of the apps I've ever installed/upgraded/uninstalled were still there!

Now, since these packages mainly consist of compressed source code, it's not going to eat up a lot of hard drive space. But, I had quite a few built up, and would rather not have them sitting there for no reason. Rather than go through and 'clean' each one individually, I created a bash script to do this for me for all packages represented in the distfiles directory. Since I know a few people who use MacPorts that aren't exactly savvy with *nix command-line tools, I thought I'd share it:

File: clean_ports

#!/bin/bash
ls -1 /opt/local/var/macports/distfiles | \
awk '{
   if ($1 !~ /^\./) {
      system("port clean --all " $1)
   }
}'

Just throw this into a file and then run chmod +x clean_ports to make it executable. Now you can just periodically run clean_ports to avoid unnecessary build-up of the remnants of MacPorts installs.

Addendum: Aaron Schaefer mentioned in the comments that this same thing can be done using MacPorts pseudo-portnames. So, the simpler solution is to do: sudo port clean --all installed