DirB, Directory Bookmarks for Bash

Inspired by browser bookmarks, DirB allows you to create directory bookmarks for moving around faster on the command line.
Looking behind the Curtain

DirB keeps all directory bookmarks in ~/.DirB, a “hidden” subdirectory of the user's home directory. When the file ~/.bashDirB is sourced from within ~/.bashrc, it checks to see whether the ~/.DirB directory exists. If the directory does not exist, it is created. This guarantees that the bookmark repository exists.

Each bookmark has an associated file in the ~/.DirB directory with the same name as the bookmark. The bookmark file contains a one-line command, such as:

$CD /home/Desktop

The shell variable $CD is set by the g and p commands to cd or pushd, respectively, and the variable is expanded by the shell when the bookmark is invoked. In essence, the command g d is transformed into cd /home/Desktop, and p d is transformed into pushd /home/Desktop.

The DirB commands are implemented as Bash functions that do some error checking, determine which action is to be performed, and then invoke a standard command. For example, the g command does a couple checks before invoking the cd command:

# "g" - Go to bookmark
function g () {
    # if no arguments, go to the home directory
    if [ -z "$1" ]
        # if $1 is in ~/.DirB and does not
        # begin with ".", then go to it
        if [ -f ~/.DirB/"$1"
            -a ${1:0:1} != "." ]
            # update the bookmark's timestamp a
            # and then execute it
            touch ~/.DirB/"$1" ;
            CD=cd source ~/.DirB/"$1" ;
            # else just "cd" to the argument,
            # usually a directory path of "-"
            cd "$1"

The function g checks to see whether there is an argument. If $1 is a zero-length string, the user is sent home with a cd invoked with no argument. Otherwise, a check is made to see if the argument is the name of a saved bookmark and the first character of the argument is not a period.

If both conditions are met, the bookmark is run as part of the current shell by sourcing the bookmark file. Before execution, the shell variable $CD is set to the cd command. source is used instead of calling the bookmark as a shell script so that the directory change will affect the current shell. A called script would have a unique shell session that would terminate after the cd or pushd. Thus, it would have no lasting effect on the current shell session.

If the argument is not the name of a bookmark, or if it begins with a period, the cd command is invoked with the argument to go to the specified directory path.

Note that the source command in the g function above starts with a variable assignment:

CD=cd source ~/.DirB/"$1" ;

Bash syntax allows a command to be preceded by one or more variable assignments.

Error Handling

Most DirB commands eventually call cd, pushd or popd to perform the requested action. If one of these standard commands encounters a problem, it issues an error message to the standard error (stderr) stream and exits with a failing return code.

Note that because bookmarks are the names of their associated files in the ~/.DirB repository, they cannot have slashes in their names. If a bookmark cannot be created (most likely due an invalid character in the name), s will print an error message to the standard error:

% s a/d
bash: DirB: /home/.DirB/a/b could not be created

An error message will result if an argument to either g or p is neither a bookmark nor a valid directory path:

% p missing
bash: pushd: missing: No such file or directory

This will occur if the bookmark name is misspelled or if the bookmark has been removed. A similar error message results from the d and r commands if their arguments are not valid names of a saved bookmark:

% d missing
bash: DirB: /home/.DirB/missing does not exist
% r missing
bash: DirB: /home/.DirB/missing does not exist

If an error is encountered, DirB commands will exit with a failing return code. This behavior allows other Bash scripts to use these functions and take appropriate action in the event of an error.


DirB was created as a set of Bash functions to extend the concepts of bookmarks to Linux directories. It accelerates the movement between frequently accessed directories from the command line or from shell scripts. Although it's a simple tool, I rely upon DirB daily and hope that others will find it useful too.

Ira Chayut is a longtime UNIX/Linux developer, having first worked on version 6 UNIX in 1976. He is the author of C and UNIX reference booklets, runs www.verilog.net, and has given talks on integrated circuit verification. Currently, he is the founder of a consumer products company and is responsible for all of the embedded and DSP programming. He can be reached at ira@dirb.info.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

you can just add in your

Anonymous's picture

you can just add in your .bash_profile

export hello=~/tmp/hello
source .bash_profile
cd $hello

you can still use the tab to autocomplete


you can just add in your

Keith Daniels's picture


I knew I could export directories like that but it never dawned on me that autocomplete would still work.... duh. One track mind....

When I tested this, I could not get autocomplete to work with cd. It would work with ls or any other program except cd. Dont' know why, probably some bashrc or profile setting I used is blocking it.


BTW I just noticed something about using cd in Ubuntu Maverick that I "think" is a new (or just something I never noticed ) feature:

If you have a sub-directory called ~/test/test and when using cd you mistakenly type:

cd ~/text/text

It will take you to ~/test/test -- instead of giving you an error message -- if that is the only directory with similar spelling on you computer.

Personally I think that is wonderful...


All the new OSs and windowing systems are oriented towards content consumption instead of content production.

--Steve Daniels 2013

Yes...! Something I never could figure out how to do...

Keith Daniels's picture

Something I always wanted and never could figure out how to do...

Good Idea!
Great Post!
Excellent Explanation!


All the new OSs and windowing systems are oriented towards content consumption instead of content production.

--Steve Daniels 2013