Categories
SysOps

How to jump between defined set of locations

I am constantly moving between a fixed list of several directories daily. Using cd can be tedious at times, so I defined a shell function to quickly jump between a 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 a 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 a defined set of directories.

$ j b
~/bin$

You can verify the defined directory using the following command.

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

Nice.