Here’s a (relatively) simple shell function for navigating up the tree from your current working directory. It was inspired by bd, but I got frustrated with some aspects (and the fact that it’s not really OS X-compatible anymore).

up allows fuzzy matching as long as the characters are in order. So, if you’re in /Users/ttscoff/Dropbox/Code/popclipextensions/Blockquote.popclipext and you type up dbox, you’ll jump up to your Dropbox folder.

There’s a helper function called _up that you can call directly to integrate into other commands. It simply echoes out the match with no newline, so you can do things like ls -1 $(_up dbox). Mix it into other functions along with something like sentaku and have some fun.

up is designed to be sourced in your login profile. This is partly because I prefer funtions to scripts scattered everywhere, but primarily because scripts run in a child process and the cd command doesn’t affect your current shell unless you source it with . up args. That’s an inconvenience.

# inspired by `bd`:
function _up() {
	local rx updir
	rx=$(ruby -e "print '$1'.gsub(/\s+/,'').split('').join('.*?')")
	updir=`echo $PWD | ruby -e "print*\/${rx}[^\/]*\/).*/i,'\1')"`
	echo -n "$updir"

function up() {
	if [ $# -eq 0 ]; then
		echo "up: traverses up the current working directory to first match and cds to it"
		echo "You need an argument"
		cd $(_up "$@")

If you’re interested in fuzzy Bash completion for up, see this Gist. It will let you type up dbx⇥ (or even up x) and expand it to up Dropbox using standard Bash completion.

Just as an aside, my target directory is very often the root level of my current git repository. This alias will jump you straight to the top level folder of any git-based project (with an update from Bernd Goldschmidt):

# gt: Go Top
alias gt='cd $(git rev-parse --show-toplevel 2>/dev/null || (echo "."; echo "Not within a git repository" >&2))'

I love z and bashmarks, too. Anything that makes moving around the system as fast and as intuitive as possible. I often jump to Terminal from Finder, move to the folder I need and type f, which I have aliased to open -a Finder .. It’s just faster, even with ⌘⇧G available. Have fun!

Ryan Irelan has produced a series of shell trick videos based on posts. Readers can get 10% off using the coupon code TERPSTRA.