Working on the Linux command line: How to use bookmarks for directories

Posted . Visible to the public.

Bookmarks for directories will be most helpful if you are forced to work in deeply nested projects. Then it's really helpful!

This makes use of the CDPATH variable. Similar to the PATH variable, which holds the list of directories which are searched for executables, CDPATH contains the list of directories that are available for cd. Besides the current directory (.), you can add others to that.

The trick is to add a directory for bookmarks to CDPATH.

First, create the directory with: mkdir ~/.bookmarks.

Then add the following to your ~/.bashrc:

export CDPATH=.:~/.bookmarks/
function mark {
  ln -sr "$(pwd)" ~/.bookmarks/"$1"
}

# Always resolve symbolic links (e.g. for bookmarks)
alias cd="cd -P" 

This will add a new symlink to your bookmarks directory for the directory you're currently in.
The alias on the last line is optional.

To make sure you're not running into conflicts with the content of your current directory, always use a prefix for your bookmarks. @ works well. This has also has benefits for the usage with autocompletion.

Example usage:

>cd Projects/makandra-cards/
./Projects/makandra-cards/
>mark @cards
>cd ~
>pwd
/home/judith
>cd @cards
/home/judith/.bookmarks/@cards
>pwd
/home/judith/Projects/makandra-cards
Judith Roth
Last edit
Judith Roth
License
Source code in this card is licensed under the MIT License.
Posted by Judith Roth to makandra dev (2021-09-17 17:20)