Skip to content

[Linux] Efficient Directory Navigation On Linux: Mastering The `z` Command

Last Updated on 2024-09-17 by Clay

Introduction

z command is something I've wanted to write about for a long time! However, I've been busy with AI training (company work) and model acceleration (personal interest), so I haven't had the time. Let's put it this way, if someone asks me to recommend essential tools for a Linux system, I would undoubtedly place z in my top ten list.

On a Linux system, we often need to open the terminal to work and interact with the system. One of the most frequently used commands is cd for navigation. Now, what if there's a tool that lets us quickly navigate to frequently visited directories without typing the full path, just a few initial letters of the folder name? Wouldn't that be amazing?

This is where the z command comes in handy: it collects the directories we frequently visit and calculates a "weight" for them, allowing us to switch to commonly used directories quickly. It's similar to cd but more intelligent and efficient.


Download and Installation

First, we can download the z.sh script. This is not for direct execution but rather for sourcing to make it effective.

$ wget https://raw.githubusercontent.com/rupa/z/master/z.sh -O ~/z.sh
$ echo "source ~/z.sh" >> ~/.bashrc
$ source ~/.bashrc


With this setup, every time we open a terminal, the z command will be automatically enabled.


Usage

The usage of the z command is very straightforward.

z <partial-folder-name>


For example, if we frequently visit the ~/projects/my_project directory, we might just need to type:

z my


And we will automatically reach that directory.

Here's an example of my usage:


We can also use the following command to list the recorded directories and their weight scores:

z -l


Output:

28314      /usr
28318 /media/clay
56608 /tmp
56895 /home/clay/Projects
227634 /home/clay/Scripts
299019 /home/clay/Projects/Any-Test


Over time, the z database might store some directories that no longer exist, essentially tracking emptiness. So we can use:

z -c


To clean up non-existent directories.

Lastly, if you want to know more about any command, you can always check it using z -h.


Advanced Usage: Setting a Blacklist

Haha, this is a treasured feature: after all, there are always some private files on our computers that we don't want accidentally revealed. If you visit them frequently, z might mistakenly give them a high weight, so what if during a demo with a client, z takes you into these folders?

Don't worry, we can use:

z -x /path/to/remove


To remove tracked directories.

You can also set environment variables in ~/.bashrc to exclude directories from z's tracking calculation:

export _Z_EXCLUDE_DIRS=("/path/to/exclude1" "/path/to/exclude2")

References


Read More

Tags:

Leave a ReplyCancel reply

Exit mobile version