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
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.
-
Plasma Desktop Will Soon Ask for Donations
The next iteration of Plasma has reached the soft feature freeze for the 6.2 version and includes a feature that could be divisive.
-
Linux Market Share Hits New High
For the first time, the Linux market share has reached a new high for desktops, and the trend looks like it will continue.
-
LibreOffice 24.8 Delivers New Features
LibreOffice is often considered the de facto standard office suite for the Linux operating system.
-
Deepin 23 Offers Wayland Support and New AI Tool
Deepin has been considered one of the most beautiful desktop operating systems for a long time and the arrival of version 23 has bolstered that reputation.