Make Your Life Easier With Z Script

When working in terminal, we often need to jump through different directories. Keeping the track of directories we’ve changed to can be a tough task. One option can be using the history command in Linux:

🚀 ~ history | grep cd

This is not an ideal solution because if the list is very long and we don’t remember the exact name of the directory we used before, then there we have problems. On the other hand, copy-pasting directory names from a list generated by the history command can be time-consuming.

Meet z.

 

How it works

First, we need to download it from a GitHub repository here: github.com/rupa/z. We only need the file called z.sh.

🚀 ~ wget https://raw.githubusercontent.com/rupa/z/master/z.sh -P ~/

I recommend to save it in the user’s home directory to easily access from our .bashrc configuration later. To make it work, we must open our .bashrc and at the end of it call&apply our z shell-script file. We can do that by running the commands below.

Important! Make sure you type greater-than sign > twice, otherwise your .bashrc will be emptied and only contain that one line. Make sure you backup your .bashrc before making any modifications.

🚀 ~ echo ". \$HOME/z.sh" >> ~/.bashrc
🚀 ~ . ~/.bashrc

Now that we have our z command installed, let’s see what it can do. But first, we need to jump around some directories so that z fills up some entries in its database.

cd dir

If we type z and hit Enter, we’ll see that z stored some values of directories we’ve changed to.

z db

And then comes the awesome part. Let’s change directory to /home/nux/Pictures/family/2020-05/Armenia again. z allows us to use keywords and also has smart autocompletion to achieve this task. With z, we can just do z Armenia or type z Ar and hit the Tab key. z will find the best match for us and change to the most frecent directory 🔗.

z keyword z autocomplete

This is just a small part of z capabilities. More information, available options, etc can be found right in the script file, as commented documentation.

View the entire file
# Copyright (c) 2009 rupa deadwyler. Licensed under the WTFPL license, Version 2

# maintains a jump-list of the directories you actually use
#
# INSTALL:
#     * put something like this in your .bashrc/.zshrc:
#         . /path/to/z.sh
#     * cd around for a while to build up the db
#     * PROFIT!!
#     * optionally:
#         set $_Z_CMD in .bashrc/.zshrc to change the command (default z).
#         set $_Z_DATA in .bashrc/.zshrc to change the datafile (default ~/.z).
#         set $_Z_MAX_SCORE lower to age entries out faster (default 9000).
#         set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
#         set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself.
#         set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
#         set $_Z_OWNER to your username if you want use z while sudo with $HOME kept
#
# USE:
#     * z foo     # cd to most frecent dir matching foo
#     * z foo bar # cd to most frecent dir matching foo and bar
#     * z -r foo  # cd to highest ranked dir matching foo
#     * z -t foo  # cd to most recently accessed dir matching foo
#     * z -l foo  # list matches instead of cd
#     * z -e foo  # echo the best match, don't cd
#     * z -c foo  # restrict matches to subdirs of $PWD
#     * z -x      # remove the current directory from the datafile
#     * z -h      # show a brief help message

 

Where z stores its database

By default, z stores its database in the user’s home directory, in the file named .z. However, we can change this by modifying the variable called $_Z_DATA (see the docs). Anytime we want to entirely clear our database, we can simply run:

🚀 ~ > ~/.z

 

Epilogue

z is a really powerful tool which can save us a lot of time. It’s available in both bash shell and Z shell (zsh). You can also install it in Windows Power Shell.

github.com/badmotorfinger/z
PS> Install-Module -Name z

 

Enjoy z life!!! Peace&Love 😊 ✌ ☮️