Skip to content
Logo Theodo

cd into the World with One Single Command

Yohan Levy3 min read

The most famous shell command is definitely cd. It allows you to navigate through your file tree.
Sometimes, reaching the targetted folder can be a painful journey leading to a laborious and infinite sequence of cd and ls.
Thankfully, some workarounds exist to make navigation less laborious, let us mention three of them:

None of these approaches is really satisfying from a web developer’s perspective as it can quickly get time-consuming.
How about we could cd into any folder with one single command? Let me introduce you Z.

Z is a shell script that will learn from your shell history in order to get you to your favorite folders with one command.
The word ‘favorite’ has to be defined here: the most frequent and recent. Z is based on the ‘frecency’ concept widely used in the Web World to build a proper and consistent URIs ranking.
To put it simply, Z attributes a mark to all the folders you have visited with your shell since Z was installed.

Installation

Are you ready to save a lot of time? Let’s install the beast.
Clone the Z github project on your machine:

git clone https://github.com/rupa/z.git

In the ./bashrc or ./zshrc shell config file, add the following line at the bottom:

. /path/to/z.sh

To end up the setup, you have to source the freshly updated shell config file:

source ./bashrc

You can also restart your terminal to enable the z command.

From now on, perform your very last cds through your file tree to help Z learn about your habits and enrich its database accordingly.
Trust me, after a day or two, you won’t use cd anymore to reach your daily working project.

CraZy Tips

Let’s take the example of Max who works on his startup website. The path of the project folder is the following one: /home/max/Documents/Very/Long/And/Painful/Path/To/My/Startup/Website
Given that Max frecently cd into it since Z installation, he can simply run the command z website to reach it. z web or even z w can do the magic too since the folder is the best match.
Z script is smart enough to change the current directory based on the regex you provided.

To get the ordered list of visited folders, type z -l in your terminal.

97.5       /home/max/Documents/path/to/Happiness
128        /home/max/Documents/path/to/Success
256        /home/max/Documents/path/to/Startup/Fundraising/Investors
408        /home/max/Documents/path/to/Startup/Legal
544        /home/max/Documents/path/to/Startup/Marketing
822        /home/max/Documents/Very/Long/And/Painful/Path/To/My/Startup/Website

As you can see, you also get the current mark of each folder. The next section deals with the Maths behind the scenes.
To retrieve information about ranking or timing, you can use the z -r or z -t commands.

The Frecency Algorithm

Frecency is a blend of ‘frequency’ and ‘recency’ words. The algorithm that computes the folders ranking is pretty simple and crazy damn powerful.
We have to consider it as a two dimensional problem with two variables: rank and time.
The rank is a metric to assess frequency whereas time is the date of folder last visit, stored as a timestamp in seconds.

First part: each time a folder is visited its rank is incremented by one unit and its time is updated.

Second part: frecency formula
If the current folder has been visited during the:

Otherwise, frecency = rank / 4

By default, Z uses the frecency to compute the best match. However, you can use -r and -t options to respectively cd into the best matching folder based only on ranking or time criteria.

Liked this article?