Good way to bookmark/navigate relative directory paths?

Posted on

Problem :

I work on a linux system with bash 4.x where I frequently find myself navigating different copies of very deep (and large) file directory structures. However, in reality I only need to access a handful of the directories for my day-to-day work.

I would love to have a way to navigate into the correct path without memorizing or typing out the destination completely. Is there a tool that would allow me to register relative bookmarks?

For example somewhere in the path prefix is the pattern


where foo is a unique indicator that I am inside one of several possible work directories specified by id_number. I would like to bookmark directories relative to foo and then type

cmd bar

where bar would auto expand into a list of bookmarks matching a pattern and maybe a tool for selecting which one to use. I am sure other user interfaces would suffice as well.

I would consider switching my shell to zsh or some alternative if this is necessary to obtain the necessary work flow.



Solution :

The shell option ‘cdable_vars’ is probably what you need. Put the code below in your .bashrc file (or whatever option file happens to be sourced by your shell). Update the bookmarks of course.

# Allow cd to use variables as arguments
shopt -s cdable_vars

When you are in the parent directory, use it as follows:


If the bookmark is an absolute path, then it can be used anywhere.

I have built a set of shell functions that does exactly what I needed.

From the README is a bash 4.x library that enables relative bookmarking of file system
directories including tab completion.

anc is designed to do stuff like this as well.

Here’s an excerpt from the README:

# make the current directory the default anchor:
$ anc s

# go to /etc, then /, then /usr/local and then back to the default anchor:
$ cd /etc; cd ..; cd usr/local; anc

# go back to /usr/local :
$ anc b

# add another anchor:
$ anc a $HOME/test

# view the list of anchors (the default one has the asterisk):
$ anc l
(0) /path/to/first/anchor *
(1) /home/usr/test

# jump to the anchor we just added:
# by using its anchor number
$ anc 1
# or by jumping to the last anchor in the list
$ anc -1

# add multiple anchors:
$ anc a $HOME/projects/first $HOME/projects/second $HOME/documents/first

# use text matching to jump to $HOME/projects/first
$ anc pro fir

# use text matching to jump to $HOME/documents/first
$ anc doc fir

# add anchor and jump to it using an absolute path
$ anc /etc
# is the same as
$ anc a /etc; anc -1

# add anchor and jump to it using a relative path
$ anc ./X11 #note that "./" is required for relative paths
# is the same as
$ anc a X11; anc -1

# using wildcards you can add many anchors at once
$ anc a $HOME/projects/*

# use shell completion to see a list of matching anchors
# and select the one you want to jump to directly
$ anc pro[TAB]

Full disclosure: I’m the author of anc.

Leave a Reply

Your email address will not be published. Required fields are marked *