Efficient navigation in bash

Navigating through a directory tree is something that can eat up a lot of time. Even though Tab completion helps to reduce that time a lot we still have to cycle through the hierarchy. Thinking about efficient navigation in a CLI is a great way to boost our efficieny.

Assume we need 20 seconds to complete every cd command. One might say it doesn’t make sense to think about reducing that time if the optimization itself may take several hours. But actually cding is one of the most common tasks in our CLI. A quick look in our history will prove it

history | grep cd | wc -l

Let’s assume that each invocation takes 20 seconds and we change directories 30 times per day. In one year we have wasted 2.5 days. That sums up to nearly an entire month within 10 years.

Bash has a lot of builtins that makes changing directories a lot more efficient.

OLDPWD

Our bash environment has a variable named $PWD that point to the current directory. The PWD variable is often used as a part of the bash prompt. When we change our working directory PWD is updated too. Another variable called $OLDPWD stores the directory we are coming from. We can use that variable as a reminder.

The following command brings us back to our old working directory.

cd ${OLDPWD}

That is still a lot to type so there is a shortcut for this.

cd -

Using that command again allows us to switch between two directories extremely fast.

DIRSTACK

Using cd - only works if we switch back and forth between two directories. But what if we do something like this.

cd ~/workspace/project/html/
cd /var/log/
cd apache2/

A cd - will bring us back to /var/log but there is no way to go back to where we originally came from.

The DIRSTACK is like a stacks of OLDPWDs.

We can push directories onto that stack with

pushd ${PATH_TO_DIRECTORY}

And take an item from the stack with

popd

In that way we can go back multiple steps instead of switching back and forth.

CDPATH

CDPATH sets the base directory for cd. It is similar to PATH except that it works in conjunction with cd.

Assume our main working directory is ~/workspace/current_project. Setting CDPATH to export CDPATH="~/workspace/" allows us to change into that directory from everywhere else with cd current_prject instead of cd ~/workspace/current_project

Note: The package bash-completion has to be installed to make Tab completion work with CDPATH.

Autocd

There is a shell option name autocd that allows us to change into a directory without the need to call cd. Actually cd is a shell builtin and we can make bash autocomplete that if we do not need it.

Fuzzy finder

A fuzzy finder like helps us to finding files not based on their location but based on their name. The fzf command allows us to find files very quickly and we can use its output as an argument for cd like so:

cd $(fzf | xargs dirname)

It probably does not make sense to use all these techniques at the same time. But thinking about our efficiency is something we should do regularly. Even though it takes some time to try new things an increased efficiency saves us an enormous amount of time in the long run.

Time is the most valuable thing a man can spend - Theophrastus

Contact