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
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.