To add to the extremes, it is enough to be able to topologically sort in some way. #!/bin/tcsh foreach origin (`pkg version -vPL= | cut -f 1 -w | xargs pkg some-topological-sort-command-can-someone-make-this -o`) make -C /usr/ports/${origin:q} clean make -C /usr/ports/${origin:q} build deinstall install clean end That should be enough. :)