How to jump between defined set of locations

I am constantly moving between fixed list of several directories on daily basis. Using cd can be tedious at times, so I defined shell function to quickly jump between defined set of directories.

Define j shell function using ~/bin/j.sh file.

#!/bin/bash
# Jump between defined set of directories
# Source: 

# bookmarks file
bookmarks_file="${HOME}/.bookmarks"

# jump function
function j {
  if [ "$1" -a -f "$bookmarks_file" ]; then
    directory=$(awk -F: "{if (\$1 ==\"$1\") print \$2}" $bookmarks_file)
    if [ "$directory" ]; then
      cd $(eval echo $directory)
    fi
  fi
}

Source created shell function inside ~/.bashrc configuration file.

$ echo "source ~/bin/j.sh" | tee -a ~/.bashrc

Create bookmarks file ~/.bookmarks.

h:~
b:~/bin
v:~/vagrant
e:/etc
blog:~/projects/blog

Use j function to jump between defined set of directories.

$ j b
~/bin$

You can verify defined directory using the following command.

$ echo $(j blog;pwd)                                                                                                                                                                     
/home/milosz/projects/blog

It is very simple yet handy solution.

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a system administrator working for a successful Polish company and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu