Advanced command-line navigation

Command Line – Navigation

© Lead Image © artqu, 123rf.com

© Lead Image © artqu, 123rf.com

Article from Issue 238/2020
Author(s):

For decades, cd has been a Linux workhorse, but modern drives sometimes call for more efficient navigation tools. Read on for alternatives for advanced command-line navigation.

Change directory (cd) is one of the first commands that Linux users discover at the command line. It is so basic that it has only a few options. An example of the Unix philosophy that a command should do one thing and do it well, cd has survived for decades with no change of which I am aware. Still, it was designed for a simpler time, when storage was scarce. On modern drives, it can involve a lot of typing of paths, especially if your directory structure is more than one level deep.

So what are the alternatives? The answers are surprisingly numerous, ranging from those built into the shell, to more recent options that may require extra setup.

Built-In Alternatives

The shell has several ways to make navigation more efficient:

  • cd's own shortcuts: Typing cd .. moves you to the current directory's parent directory. A tilde (~) moves you to your home directory and a hyphen (-) to the previous directory (Figure 1).
Figure 1: The cd command contains several shortcuts.
  • The print working directory (pwd) command can be used to change elements of the directory name using the structure cd ${PWD/OLD/NEW}.This structure is mostly useful for changing between directories in the same level of the directory hierarchy. It can also be effective in scripting (Figure 2).
Figure 2: pwd can be a quick way to jump to another directory.
  • The Bash history can be navigated with arrow keys or by specifying the number of an item in the history with !NUMBER. If you have recently switched to a directory, this may be an alternative.
  • Pressing the Tab key produces possible completions. Eventually as you type, fewer completions are left until only one remains, and it is automatically entered.
  • Symbolic links can have a shorter name than their original reference. Creating a series of symbolic links as children of your home directory creates a do-it-yourself navigation structure with minimal typing.

While these built-in alternatives improve on cd's efficiency, the alternatives listed below have more functionality and are often more efficient. For some, these alternatives' main shortcoming is that they require some configuration to be useful.

autojump

autojump [1] records the directories you switch to using cd and creates a database of shortcuts. It requires a somewhat complicated setup. In Fedora and related distributions, you will need to enable the Extra Packages for Enterprise Linux (EPEL) repository that includes autojump [2]. Most distributions enable autojump upon installation, but before using Debian and its derivatives you must run the following command:

source /usr/share/autojump/autojump.sh on startup

To permanently enable autojump, you can add the same command to ~/.bashrc so that autojump is available whenever you open a Bash shell.

Beware: If you are using the Zsh shell in any distribution, you will need to find the Zsh version of autojump. Without this addition, autojump will not work in Debian. You also have the option of installing from source by cloning from a user account with the command

git clone git://github.com/joelthelion/autojump.git

and then changing the permissions on the file install.py and running it as root.

Once autojump is set up, it gradually adds directories to its database (~/.local/share/autojump/) as you navigate the command line. Alternatively, you can add directories manually with the option --add DIRECTORY (-a). Either way, when a directory is added to autojump, you can switch to it using either the command autojump or simply j. Since you only have to type the directory name and not the complete path, you can save keystrokes, especially if you have a deep directory structure. For instance, instead of typing

cd /home/bb/work/journalism/2020/August

I could type simply

j August

Should you choose, you can also open a directory in the default file manager of your desktop environment using the command jo DIRECTORY. If a directory is not a top-level one, then the command is jc DIRECTORY. Depending on the speed of your machine, the desktop file manager may take a few seconds to open. You can also use --complete to enable tab completion in your current session.

You can view the database with the option --stat (Figure 3). You will notice that directories are weighted according to how many times you switch to them. You can use the options --increase WEIGHT DIRECTORY (-i) and --decrease WEIGHT DIRECTORY (-d) to help eliminate any confusion between similarly named directories and --purge to remove paths that have been removed. Looking at the autojump_errors file can help you detect duplicate path names.

Figure 3: autojump maintains a database of directories, assigning each a priority according to how many times you change into it.

autojump is unable to work with files that start with a hyphen. Otherwise, after it is set up, autojump is a powerful navigation tool.

cdargs

cdargs [3] is similar to autojump, except that its documentation refers to directory bookmarks. Similarly to autojump, cdargs installs by evoking one of the scripts provided in its package, either in the current session with the command

source /usr/share/doc/cdargs/examples/cdargs-bash.sh

or permanently by adding the same command to your bash.rc file.

You can build a navigation system by changing to a directory and running the command ca, which will create a bookmark with the same name as the directory, or ca BOOKMARK, which can provide a shorter name. Alternatively, you can run cv and use a to add the current directory by its full name, d to delete the highlighted directory, and e to edit the list in your default text editor. You can close any list window with q.

To use cdargs to navigate, use the command cdb BOOKMARK. If you do not specify a directory, cdb opens a list of directories from which to choose. You can also enter cdb Lc to select by highlighting an entry on a list of directories and pressing the Enter key (Figure 4).

Figure 4: You can choose cdarg's bookmarks from a list.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Command Line: autojump

    Autojump is a mature and widely available command-line tool for navigating your directory structure. We show you how it works.

  • bd, autojump, and Fasd

    Bd, autojump, and Fasd improve the workflow for command-line aficionados thanks to quick navigation in the filesystem.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News