How to speed up directory navigation in a Linux terminal

As useful as navigating through directories from the command line is, rarely anything has become as frustrating as repeating over and over "cd ls cd ls cd ls ..." If you are not a hundred percent sure of the name of the directory you want to go to next, you have to use ls. Then use cd to go where you want to. Hopefully, a lot of terminals and shell languages now propose a powerful auto-completion feature to cope with that problem. But it remains that you have to hit the tabulation key frenetically all the time. If you are as lazy as I am, you will be very interested in autojump. autojump is a command line utility that allows you to jump straight to your favorite directory, regardless of where you currently are.

Install autojump on Linux

To install autojump on Ubuntu or Debian:

$ sudo apt-get install autojump

To install autojump on CentOS or Fedora, use yum command. On CentOS, you need to enable EPEL repository first.

$ sudo yum install autojump

To install autojump on Archlinux:

$ sudo pacman -S autojump

If you cannot find a package for your distribution, you can always compile from the sources on GitHub.

Basic Usage of autojump

The way autojump works is simple: it records your current location every time you launch a command, and adds it in its database. That way, some directories will be added more than others, typically your most important ones, and their "weight" will then be greater.

From there you can jump straight to them using the syntax:

autojump [name or partial name of the directory]

Notice that you do not need a full name as autojump will go through its database and return its most probable result.

For example, assume that we are working in a directory structure such as the following.

Then the command below will take you straight to /root/home/doc regardless of where you were.

$ autojump do

If you hate typing too, I recommend making an alias for autojump or using the default one.

$ j [name or partial name of the directory]

Another notable feature is that autojump supports both zsh shell and auto-completion. If you are not sure of where you are about to jump, just hit the tabulation key and you will see the full path.

So keeping the same example, typing:

$ autojump d

and then hitting tab will return either /root/home/doc or /root/home/ddl.

Finally for the advanced user, you can access the directory database and modify its content. It then becomes possible to manually add a directory to it via:

$ autojump -a [directory]

If you suddenly want to make it your favorite and most frequently used folder, you can artificially increase its weight by launching from within it the command

$ autojump -i [weight]

This will result in this directory being more likely to be selected to jump to. The opposite would be to decrease its weight with:

$ autojump -d [weight]

To keep track of all these changes, typing:

$ autojump -s

will display the statistics in the database, while:

$ autojump --purge

will remove from the database any directory that does not exist anymore.

To conclude, autojump will be appreciated by all the command line power users. Whether you are ssh-ing into a server, or just like to do things the old fashion way, reducing your navigation time with fewer keystrokes is always a plus. If you are really into that kind of utilities, you should definitely look into Fasd too, which deserves a post in itself.

What do you think of autojump? Do you use it regularly? Let us know in the comments.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.

Adrien Brochard

I am a Linux aficionado from France. After trying multiple distributions, I finally settled for Archlinux. But I am always trying to improve my system by stacking up tips and tricks.
Your name can also be listed here. Write for us as a freelancer.

Latest posts by Adrien Brochard (see all)

16 thoughts on “How to speed up directory navigation in a Linux terminal

    • Autojump looks decent, but I like wcd; notice that it works on many platforms, and with minor changes in the way the program is set up initially, it will perform similarly - and in many cases - identically, even when using different shell interfaces on different operating systems. To me that is more powerful than autojump, though autojump does look like a handy tool.

      Several shells have some notion of a directory "stack", including zsh, bash, and the features found in wcd. Before I knew about any of these, I had a series of KSH scripts, which turned out to be reasonably portable, pretty close to a POSIX-like syntax, so they've worked with little or no modification on Bash as well.

  1. I don't need anything, I just use shopt -s autocd and you never have to type cd again! tehehe XD

      • alright, I ran "shopt -s autocd" back and then cd command got enabled again. that script or whatever it is doesn't have a manpage so beginners could reinstall the hole system just because they don't know how to fix the trouble so you'd better not write this sort of comments. please sombody remove that silly comment

        • neyson, lesson learned I hope. You don't just type in commands to a Linux terminal without knowing the effect, especially when you find them on a random blog. Either ask or research.

        • Assuming you are using bash(1) as your shell, shopt is a shell "built-in" command, and is documented in the man page for bash. (In some other, more civilized shells, the which command is a built-in that itself aware of shell built-ins, and 'which shopt' would have reported that fact to you, but AFAIK, bash does not have a built-in which.)

          Also, I'd amplify djr's comment: Never type in a command (whether in Linux or elsewhere) without first learning what it does.

  2. autojump is in Gentoo’s official repository as well. It’s masked, but this is not a problem for a real Gentoo Linux user, is it?

  3. Try the tree command piped through more (or less, as you prefer) to get a look at the entire structure. See tree's man page for good options.

  4. Who really does cd ls cd ls...? I mean, the tab key is really useful for that. So it's not *that* issue that this is solving. Instead, it's just super convenient and quick to not have to find out the directory structure at all.

  5. # I snagged these functions way back in the nineties when I was using UNIX systems and the Korn Shell.
    # Before leaving that UNIX shop, I found that these same routines worked on Bash shells too, so I made sure
    # to create and keep a copy of these on several systems, including mail servers so that I could grab them
    # any time and bring them with me.

    # These do interesting things too. For instance, g simply goes to your home directory and prints the current
    # working directory name (which is your home directory). gn allows you to list the directories in your current run,
    # then select the directory you want to access by number (or by name). gb goes back one directory in the stack.
    # up allows you to navigate up the /dir1/dir2/dir3.../dirn hierarchy until you reach /. There are plenty of other
    # combinations.

    # Today there are plenty of tools like wcd and there are powerful shells, such as zsh, and even the latest releases
    # of bash do more than you may realize. Still, you can use this 2-3 decade old script on many systems and it
    # works even today on 2014-vintage shells too.

    # directory stack functions

    declare -i DNUM=0
    DLIST[DNUM]=`pwd`

    function g # go to a directory
    {
    if builtin cd "$@" >/dev/null && [ ${DLIST[DNUM]} != "$PWD" ]
    then
    DNUM=DNUM+1
    DLIST[DNUM]=$PWD
    fi
    pwd
    }

    function gb # go back
    {
    if (( $DNUM > 0 ))
    then
    DNUM=DNUM-1
    fi
    g ${DLIST[DNUM]}
    }

    function gn # go to selected (nth) dir
    {
    select DIR in `echo ${DLIST[*]} | tr " " "12" | sort -u -y0`
    do
    if [ "$DIR" ]
    then
    g $DIR
    else
    g $REPLY
    fi
    break
    done
    }

    function up # go up n levels
    {
    declare -i levels

    levels=${1}

    if [ -z "${1}" ] && [ ${PWD} != "/" ]
    then
    g ..
    return $?
    fi

    while [ ${levels} -gt 0 ] && [ ${PWD} != "/" ]
    do
    g ..
    levels=levels-1
    done
    }

  6. If you have a relatively small number of often-used "top" directories, then CDPATH is an option; eg. CDPATH=.:$HOME:/var/www:/var/www/websites

Leave a comment

Your email address will not be published. Required fields are marked *

Current ye@r *