Jump! Quick directory change at the command line

Jump! Quick directory change at the command line

Article from Issue 207/2018
Author(s):

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).

Figure 1: The bd script quickly takes you back to the directory tree and allows additional commands to be integrated.

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

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.

  • Beyond cd

    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.

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