Flexible Directory Navigation with Bash

I recently came to the conclusion that I was spending too much time at the Linux Terminal command line navigating to my Rails Projects and knew there had to be a better way.  All of my projects have the path /mintland/webapps/[PROJECT]. Given that all project folders had the same root path, it should be easy.

I needed a script to change to any [PROJECT] directory allowing me to type something like

$ ./myproj.sh [PROJECT]

which would change to the project folder I entered as a parameter.

Bash changes directories in the script’s subshell, so I also needed a solution that would take me directly to the project folder without entering

$ . ./myproj.sh [PROJECT] (or $ source ./myproj.sh [PROJECT])

Enter .bashrc, the Bash Shell script run each time a new terminal window (bash instance) is loaded.  I created two functions, rdir() which changes to the project directory, and rproj() which changes to the project directory and fires up Sublime Text.

rdir()
{
webappDirectory=”/mintland/webapps/”$1
cd $webappDirectory
clear
}

rproj()
{
webappDirectory=”/mintland/webapps/”$1
cd $webappDirectory
clear
subl $1.sublime-project &
}

If the project folder name was “demo_app”, the command to change into the project folder would be

$ rdir demo_app

You can see how the “demo_app” parameter is processed in the bash script with the “$1” input variable.

If you’re curious about the last line in rproj(), I use a [PROJECT].sublime-project file for each of my projects when loading Sublime Text, so when executed for our demo_app project example the line would be

subl demo_app.sublime-project &

Now THAT’S flexible directory navigation!