Navigating with Elegance

Starlight Romero
3 min readMay 3, 2021

My dad used to tell me, “there’s always more than one way to get somewhere.” He used to take me with him everywhere via streets, dirt roads, freeways, back alleys, and highways. He would hardly ever take the same way twice. This was not only a key factor in nurturing my sense of direction but also made me realized that the knowledge of how to get somewhere is powerful. While most people cannot make a turn without the consensus of Siri, I prefer to go different ways and figure it out. Eventually, I’ll arrive at my destination with more knowledge of how to get there than before.

Getting Home

A similar experience happened when navigation through directories in my terminal. The de-facto command for changing directories is cd .

$ cd Projects

Before we were at the user’s home directory. How can we get back?

$ cd ~

The tilda expands to $HOME in shells (bash, zsh, etc.) Therefore we can also do the expanded command.

$ cd $HOME

Of course if all we want to do is get to the home directory we can use cd

$ cd

Moving Backwards

Suppose we want to go back to the last directory. Right now we are in this file path ~/Projects/learning-golang/concurrency

We can go back up to the learning-golang folder using dots

$ cd ..

But what if instead we want to reference some notes?

From ~/Projects/learning-golang/concurrency

$ cd ../notes/concurrency

We now sit in the directory ~/Projects/learning-golang/notes/concurrency . After looking at our notes we want to go back to the ~/Projects/learning-golang/concurrency directory. We can achieve this in one of three ways.

From where we are currently

$ cd ../../concurrency

From our $HOME directory

$ cd ~/Projects/learning-golang/concurrency

Or the most elegant route

$ cd -~/Projects/learning-golang/concurrency

The command cd -is a shorthand for $OLDPWD . $OLDPWD is a variable which holds the value of the previous directory. The shell keeps track of and changes this value. You can check this value at anytime

$ echo $OLDPWD/home/starlight/Projects/learning-golang/notes/concurrency

Following the Trail

What if there was a way we can keep track of the path we have traversed? Perhaps this path can be stored in the structure of a stack, a directory stack.

We will start at our $HOME directory.

$ pushd Projects$ pushd learning-golang$ pushd concurrency

The command pushd adds the directory to the directory stack at index 0 and changes into it. We can then print out the directory stack.

$ dirs~/Projects/learning-golang/concurrency ~/Projects/learning-golang ~/Projects ~

We can also see the directory stack as an indexed list.

$ dirs -v0	~/Projects/learning-golang/concurrency
1 ~/Projects/learning-golang
2 ~/Projects
3 ~

We can use the command popd to pop off the top of the directory stack, index 0, and change into the directory at index 1.

$ popd

Let us go back to into the concurrency directory.

$ pushd concurrency

Once again we want to check on some notes at ~/Projects/learning-golang/notes/concurrency .

$ pushd ~/Projects/learning-golang/notes/concurrency

However this time we also need to check some notes which are in the directory ~/Projects/learning-golang/notes/pointers-and-errors .

$ pushd ~/Projects/learning-golang/notes/pointers-and-errors

The directory stack print out shows the following.

$ dirs -v0	~/Projects/learning-golang/notes/pointers-and-errors
1 ~/Projects/learning-golang/notes/concurrency
2 ~/Projects/learning-golang/concurrency
3 ~/Projects/learning-golang
4 ~/Projects
5 ~

Now to get back to ~/Projects/learning-golang/concurrency we cannot simply do cd - since we have traversed into two separate directories. We can instead use popd .

$ popd~/Projects/learning-golang/notes/concurrency ~/Projects/learning-golang/concurrency ~/Projects/learning-golang ~/Projects ~$ popd~/Projects/learning-golang/concurrency ~/Projects/learning-golang ~/Projects ~

We are now in concurrency and can print out our directory stack to see what happened.

$ dirs -v0	~/Projects/learning-golang/concurrency
1 ~/Projects/learning-golang
2 ~/Projects
3 ~

The directory stack has shrunk and at index 0 is our current directory.

Going the Distance

Go forth now and traverse your directories with the knowledge of an ancient sailor who uses the stars.

--

--