How to Create and Use Bookmarked Directories in the Terminal (+Video)

Timo Kats
Level Up Coding
Published in
3 min readFeb 25, 2024

--

Photo by Ilya Pavlov on Unsplash

The programmer lives in the terminal. As a result, optimizing your workflow in the terminal is very important if you’re a programmer. A good place to start with this optimization is the commands you use the most.

According to the internet, cd is the most used command in Linux. This command is used to change directories (traverse your filesystem). Hence, in this article, we will optimize the usage of this command by adding the ability to add/manage/use bookmarks.

CD commands without bookmarks

Out of the box, cd is a very basic command. In fact, you just type cd and add the path to whatever folder you want to go to. This can be an absolute path or a relative path. Moreover, there are some shortcuts available. For example, just typing cd will direct you to your home folder.

An annoying property of this simple command is that you always need to know/type the entire folder path of where you want to go. Or, you traverse your file system in steps. Both of these scenarios can be time consuming and tedious. Especially due to the amount of times you have to use this command.

>~$ cd..
>~$ bash: cd..: command not found
>~$ cd Code/
>~/Code$ cd PHP/
>~/Code/PHP$
>~/Code/PHP$ cd web/
>~/Code/PHP/web$

Bookmarking directories

As a result, if you often work in the same directories (and have to travel between them), it could be worthwhile to go there directly with one command. For this, you can use cdb (CD with bookmarks).

CDBookmark has 4 core functionalities: add, delete, list, and <bookmark>. First, cdb add <bookmark> will add a bookmark to your current directory (i.e. what you would get if you’d run pwd) to your bookmarks under the name <bookmark>.

Next, the command cdb <bookmark> will cd to the directory you bookmarked under the name <bookmark>. Finally, you can delete a bookmark with the command cdb delete <bookmark> and list your currently saved bookmarks with the command cdb list.

A short demonstration of how to use these commands in a practical scenario is available in the video linked to below.

Demo (2 minutes)

Installation

Interested? Unfortunately, you need to manually install the shell script and add it to your commands to use it. Fortunately, this process is not very difficult. First, get the source code from GitHub with (for example) git clone

git clone https://github.com/TimoKats/CDBookmark.git

Next, you can add this command to your .bashrc to create an alias that points to the location of the script you just downloaded with git clone. You can typically find this .bashrc file in your home folder on any Linux system.

alias cdb='. /<path>/<to>/<cdbookmark>/<copy>/cdb.sh'

Finally, update your bash commands.

source .bashrc
# or source ~/.bashrc if you are not in your homefolder

And that should do the trick! Congratulations, you can now add/manage/use bookmarks in Linux. Finally, if you’re interested, you can have a look at the GitHub repository with the following link. If you have any ideas (or if you want to add to open source projects in general), feel free to fork the repository and continue development. Thank you for reading my article.

--

--