Linux/OSX shell trick: Create bookmarks so you can cd straight into the dirs you use regularly

Marko Lukša
2 min readJun 6, 2018

--

If you spend a lot of time in a Linux or OSX shell, you probably invoke the cd command with long directory paths more often than you’d like. Even with auto-completion, it’s still way too much work if you’re like me and you’re lazy AF.

And what drives me crazy the most, is the fact that I’m constantly typing the same bunch of paths like:

cd ~/projects/go/src/k8s.io/kubernetes
cd ~/projects/go/src/github.com/kubernetes-incubator/service-catalog
cd ~/projects/jboss/ce/application-templates
...

While there are just a few locations I regularly cd into, they’re all very long and I’m sick of all that typing. Can’t I just bookmark them somehow?

Here’s the solution I came up with.

The CDPATH solution

Even if you’re a novice shell user, you probably know what the PATH environment variable does. But there’s also a CDPATH variable, which does the same thing, but for thecd command.

For example, if you define CDPATH like this:

export CDPATH=~/projects/go/src

You can then type cd k8s.io inside any directory and you’ll end up in the ~/projects/go/src/k8s.io directory.

Of course, you can specify multiple paths inside the CDPATH variable, just like with PATH, so you could just add all the parent directories of the target directories you want to access directly.

While that might be enough for most folks, what I’m really interested in is having bookmarks for the few directories I use regularly (and not all their siblings).

Combining CDPATH with symbolic links

So, I had another brainwave and created a ~/CDBookmarks directory with symbolic links to the directories I use the most. In my case, something like:

$ cd ~/CDBookmarks
$ ln -s ~/projects/go/src/k8s.io/kubernetes/ k8s
$ ln -s ~/projects/go/src/github.com/kubernetes-incubator/service-catalog service-catalog
$ ln -s ~/projects/jboss/ce/application-templates templates
...

The remaining thing to do is point CDPATH to the bookmarks dir:

export CDPATH=~/CDBookmarks

Voilà! I can now jump into any bookmarked directory from anywhere:

$ cd k8s
$ cd service-catalog
$ cd templates

But I haven’t even mentioned the best part yet. This thing even has auto-completion! You don’t have to type the full name of the bookmark (e.g.service-catalog); just type two or three characters and press TAB:

$ cd ser<TAB>
/home/luksa/CDBookmarks/service-catalog

Now to figure out what to do with all the time & keystrokes this saves me…

--

--