Advanced command-line navigation
Command Line – Navigation
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: Typingcd ..
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).
- 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).
- 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.
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).
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.