Read more

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

Deleted user #4117
September 17, 2021Software engineer

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

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

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
Posted to makandra dev (2021-09-17 19:20)