Table of Contents

How to easily jump to most frequent directories in Linux

If you like Unix, you might be using cd command quite a lot. Lot of time, we spend in jumping different directories. In Linux, there are quite a few utilities to jump around directories. Using these utilities, one doesn't need to type in the complete path of already visited directories.

  1. We would discuss an open source utility called z which is available on the github

Lets first clone the git repo...

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

Once it is cloned, lets go to directory z and do ls -lrt

-rw-r--r-- 1 root root 8878 Nov 6 02:28 z.sh
-rw-r--r-- 1 root root 5000 Nov 6 02:28 z.1
-rw-r--r-- 1 root root 5330 Nov 6 02:28 README
-rw-r--r-- 1 root root  59 Nov 6 02:28 Makefile

Easiest way to install this utility to is to run following command...

. /root/z/z.sh

To make sure, it is enabled every time you open a new xterm, add the above line to your .zshrc or .bashrc

In my case /root/z/z.sh is the path to my z.sh, which could be different for yours. Dont forget the dot in the beginning...

Once you run the above command. z is enabled. Now lets do help on this...

 z -h
z [-cehlrtx] args

I will talk about couple of switches in brief below. z will record the directories you go to. The more the number of times, you visit a directory, z will put that directory higher in the priority of directories visited. Lets do an example. Lets create two directories directory test1 and directory test2 in root.

 ➜ ~ mkdir test1
 ➜ ~ mkdir test2
 ➜ ~ cd test1
 ➜ test1 cd ../test2
 ➜ test2 cd ../test1
 ➜ test1 cd ..
 ➜ ~ cd test1

As shown above, we entered test1 twice whereas test2 only once. If we do z -l test, it will show test1 has higher priority than test2 as shown below...

➜ test1 z -l test
4     /root/test2
12     /root/test1

Now if we just type z test, it should go to test1, since it has higher priority than test2. lets check...

➜ test1 cd /home
➜ /home pwd
/home
➜ /home z test
➜ test1 pwd
/root/test1

Which is indeed the case.

Another great benefit of using z is its tab functionality. Suppose you want to scroll through the list of directories before choosing the right one. You can type the name of directory and hit tab instead of enter, it will show the list of all visited directories containing the pattern on which hit tab

z py
/home/dbfiles/server_similarto_data/projects/pythonScripts /home/projects/pythonScripts
/home/downloads/Python-3.7.0                /usr/local/lib/python3.7/site-packages/sky/view

Once it shows the above list, keep hitting the tab to choose the right directory and then hit enter, it will take you to the directory that you selected.






Related Posts