---
title: Intrepid command line directory traversal
date: 2015-04-01 08:37
slug: intrepid-command-line-directory-traversal
categories:
- Code
tags:
- bash
- terminal
- unix
---

[fzf]: https://github.com/junegunn/fzf

![](https://brettterpstra.com/uploads/2015/04/cd-directory-traversal.jpg)

I use Terminal (well, [iTerm 2](http://iterm2.com/)) for file management on my Mac more often than I use Finder. Typing out long path names is often more tedious than drilling through Finder folders, though[^g]. I have enough tag-based Spotlight (`mdfind`) tools worked out in Bash that traversal in the shell has most of the same conveniences[^mdfinds], but still, some of those paths...

There are a lot of solutions available for efficient directory navigation in the shell. Here's a quick rundown of my favorites.

[^g]: to be fair, even in Finder I'm usually using ⌘⇧G and tab completing paths anyway...

[^mdfinds]: Did you know that `mdfind -s [saved search name]` will list the contents of a "Smart Folder" created in Finder?

### Fuzzy history matching

There have been a few great solutions for jumping back through your history. From the Bash builtins `pushd`, `popd`, and `dirs` (see `help [command]`), we've progressed to tools like [autojump](https://github.com/joelthelion/autojump) and [z](https://github.com/rupa/z). I'm partial to [fasd](https://github.com/clvv/fasd), which is currently aliased to both `j` and `z` because MUSCLE MEMORY.

```bash
alias z="fasd_cd -d"
```

With these tools, every directory you `cd` to is remembered, and you can jump back to them with just fragments of their name. For example, if you were recently (or ever) in `~/Downloads/LaunchBar Actions/Forecast/`, you can just type `z forecast` to get back there.

### Bookmarking

I've been using a slightly customized version of the [bashmarks](https://github.com/huyng/bashmarks) (and [zshmarks](https://github.com/jocelynmallon/zshmarks)) system for a while for assigning quick aliases to commonly-used directories. Thanks to Jack at [OneThingWell.org](http://onethingwell.org), I recently discovered [Apparix](http://micans.org/apparix/), which is looking to be a more powerful solution. It provides additional utilities and (fuzzy) tab completion not only for the bookmark name, but for subdirectories off of the bookmark's path.

Once again, I've aliased all of its features to the same commands I've been using for bashmarks. Creature of habit.

By the way, if you use bashmarks, here's a handy trick (which you could also port to Apparix using the `~/.apparixrc` file). I use the alias `m` for `save`, so bookmarking the current folder is just `m [name]`. To avoid bookmarking a folder twice with different names, I have an alias `m?` to check and see if it's already bookmarked. It uses the `l` command (list bookmarks) and greps for the current working directory:

```bash
alias m?='l|grep "$(pwd)"'
```

### One of my own solutions...

Here's one of my own functions for jumping to subdirectories up to three levels from the working directory using fuzzy search. It incorporates [`fzf`][fzf], which is brilliant and you should have it anyway.

```bash
# choose cd dir from menu (fzf)
# fuzzy search 3 levels deep
cdd() {
	local needle=$(echo "$*" | sed -E 's/ +/.*/g')
	cd "`find . -type d -maxdepth 3 | grep -Ei "${needle}[^/]*$" | fzf -s 20 -1 -0 -q "$1"`"
}
```

With this function you can drill into a directory tree quickly. If you're in `~/Code/marked` and you want to get to `~/Code/marked/vendor/multimarkdown/`, you can just type `cdd mmd` and it will find the target and jump to it. If there are multiple matches, you'll get the `fzf` screen where you can use arrow keys or typeahead filtering to pick the correct target.

Just add it to your `~/.bash_profile` or any file sourced there. It's not the fastest solution, but it doesn't require bookmarking or that the target be a frequently used directory. In combination with [`up`](/2014/05/14/up-fuzzy-navigation-up-a-directory-tree/), it makes traversing project folders simple.

I also tend to combine Finder and Terminal navigation using my `cdf` and `f` functions. See more about those in [this post](/2013/02/09/quick-tip-jumping-to-the-finder-location-in-terminal/).

[LaunchBar](http://obdev.at/launchbar/) can also be very handy as a shortcut for acting on a file or folder without digging into [`open` commands](/2014/08/06/shell-tricks-the-os-x-open-command/):

```bash
# Select the current directory in launchbar, optionally a file
lb () {
    if [[ $# = 1 ]]; then
        [[ -e "$(pwd)/$1" ]] && open "x-launchbar:select?file=$(pwd)/$1" || open "x-launchbar:select?file=$1"
    else
        open "x-launchbar:select?file=$(pwd)"
    fi
}
```

Happy (`cd`) trails.

---

Brett is a writer and developer living in Minnesota, USA. You can follow him as ttscoff. This content is supported by readers like you. [More about Brett](https://brettterpstra.com/contact.md), including social links.

Copyright © 2026 Brett Terpstra. All rights reserved.
