Jump! Quick directory change at the command line
Jump! Quick directory change at the command line
Bd, autojump, and Fasd improve the workflow for command-line aficionados thanks to quick navigation in the filesystem.
With a little practice, working at the command line lets you feel the true power of Linux. However, the standard tools are not always as convenient as they are powerful, or as the user would hope.
If you frequently work at the command line, you most likely use the cd
"change directory" command on a daily basis. In this article, I introduce a few helpers that promise greater convenience and speed, especially when changing directories in deeply nested paths. Bd, autojump, and Fasd are available as tools for different shells.
Inconvenient
The various Linux shells already provide some help with improved navigation in the command-line directory jungle, starting with finding out where you are. The pwd
command helps by outputting your working directory's path. If you now want to jump up one or more levels, you can usually do this with:
cd ..
The two periods mean one step up in the direction of the root. You can jump up two steps by using:
cd ../..
However, frequent directory changes involve much typing, which can lead to errors. If you have to switch often between two directories in different places in the directory tree, the cd -
command helps by taking you to the last directory you visited.
The directory stack provides further assistance: Multiple directories can be piled onto a stack. Bash provides the pushd
, popd
, and dirs
commands to navigate in this stack [1]. If you replace cd
with pushd
for the directory change, the shell always places the directory to enter at the top of a stack. It remembers the stack – so you don't need to – and displays it after the command. Using popd
, you can work your way back up the stack.
For orientation, the dirs -l -v
command displays the entire stack. To create the stack, you first have to access the directories with pushd
to add them to the stack, which proves useful if you have to switch between a few directories in a working session: The tool consecutively numbers the stack, and you can jump to the folders with the use of these numbers.
However, bd, autojump, and Fasd offer a more convenient approach; all the major distributions have them in their repositories. The whohas
command, installed via your package manager, tells you where to find them. Typing whohas autojump
gives you a list of distributions with the package, including the version and the respective branch.
You also can set up the bd [2] script for many distributions via a package manager. After installing, enter the two commands from Listing 1 to facilitate running bd. If bd is not available in the Linux derivative that you use, install it manually following notes from the project site on GitHub.
Listing 1
Running bd
Shorter Return Path
Imagine that you have used cd
to enter the /home/fritz/foo/bar/bat/test/bd/
directory and now want to go back to /home/fritz/foo/bar/
. You would usually enter:
cd ../../../
With the bd script you just installed, bd bar
would do the trick: bd supports autocompletion, so you do not have to type out the directory names; usually you just need two or three letters.
Bd only operates backward, not forward. But it can also be used with other commands, for example: ls
, du
, zip
, and tar
. The command
ls -l `bd ba`
lists the content of the /bar
directory, even though you are still in /bd
. The call
du -cs `bd fr`
displays the size of /fritz
(Figure 1).
autojump
Autojump [3] makes use of the basics of the directory stack described above and further expands on them. It also facilitates navigation in the directory tree in both directions, unlike bd. Autojump can be used under Linux, Mac OS, and Windows with the Bash (from version 4.0), Zsh, and fish shells and experimentally with the tcsh and Clink shells.
Most distributions offer autojump packages. Under Debian and its subsidiaries; you can add the tool to the system using the command from Listing 2. Autojump works with a database that you initially have to fill by working with cd
for a while. Alternatively, you can target folders that you use often.
Listing 2
Installing autojump
You will find the database under ~/.local/share/autojump/autojump.txt
. You can query the contents of the database at any time using j --stat
. The j --purge
call removes directories that no longer exist from the database.
If you also want to test Fasd [4], the final helper discussed in this article, it makes sense to install it now, because it also benefits from the actions for filling the database. To do this, type the entries from Listing 3 after the install.
Listing 3
Installing Fasd
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
-
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.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.