Is there something like bash's reverse-search-history (Ctrl-r), but for only directories?

I have some deep folder hierarchies that I want to jump to, so I'd like to use something like reverse-search-history, but it only looks for folder names and gives me absolute paths.

Essentially, it would give similar results to using !? but only matching commands with cd in the front, you can step through results, and full paths.

So far, the best solution I've found is bashmarks.

  • 1
    I recently started using z and that seems to work so far, but I used it for a few paths. – Rob Jun 20 '11 at 17:04

10 Answers 10


Have a look at autojump:

One of the most used shell commands is “cd”. A quick survey among my friends revealed that between 10 and 20% of all commands they type are actually cd commands! Unfortunately, jumping from one part of your system to another with cd requires you to enter almost the full path, which isn’t very practical and requires a lot of keystrokes.

autojump is a faster way to navigate your filesystem. It works by maintaining a database of the directories you use the most from the command line. The jumpstat command shows you the current contents of the database. You need to work a little bit before the database becomes usable. Once your database is reasonably complete, you can “jump” to a commonly "cd"ed directory by typing:
j dirspec

| improve this answer | |

There is

cd -

that is "cd[space][hyphen]" command, which goes to the directory you were before, essentially a "history of depth 1". Repeated "cd -" switches back and forth between two directories.

Quoting man page:

The following operands shall be supported: [...]

When a [hyphen] is used as the operand, this shall be equivalent to the command:

      cd "$OLDPWD" && pwd

Unfortunately, I don't know of a real built-in directory history.

| improve this answer | |
  • There is pushd dir popd pair which can store your history stack of directories. This is really helpful in scripts that operate in different folders. – Jakuje Oct 25 '15 at 18:59
  • I know of pushd and popd, and it is also mentioned here already. – apurkrt Oct 25 '15 at 21:19

bash has pushd/popd/dirs. I have this in my .bashrc to auto-push directories onto bash's stack.

#let cd also pushd directories into stack. Use popd to reverse stack
function cd ()
  if [ -e $1 ]; then 
    pushd $1 &> /dev/null   #dont display current stack 

Pop these using popd and display the stack using dirs

| improve this answer | |

I have had good experience with z-jump It allows completion, although only for the final destination, not stepping through a path. It does show the full path upon tab completion however.

| improve this answer | |

Just to chime in with my own experience, I wrote a simple script to address this requirement some time ago, it overrides the builtin cd command with a simple function which appends the new directory location to a history file, a python script is then used to provide a bash interface which dynamically updates an ordered list of directories as you enter search terms, somewhat like the reverse command search of bash.

It's available on git-hub for those that are curious.

| improve this answer | |

I'd like to recommend my ltcd for quick navigation through directory history:


cd demo gif

It provides the following features to make life easier:

  • Global dir listing, which shows recently visited dirs from all terminal tabs/windows.
  • Local dir listing, which is local to current shell session.
  • Both listings support quick navigation by using j/k (go down/up), numbers, and word searching.
  • Global free jumping (e.g. "cd dir" or "cd ar" to go to /path/to/foo/bar/directory/).
| improve this answer | |

I have made a script that has similar functionality to oh-my-zsh's dirs -v command that works on bash. If you have ever use oh-my-zsh, you might have noticed that the directory history provided by the command dirs -v will be reset every time you exit the terminal. It won't happen if you use this script, however.

The functionality:

  • Show the list of 10 most recent used directory with d.

  • Jump to any directory in the list by typing the number of the directory in the list. You need to use 0 instead of 10 to jump to the 10th directory.

  • A directory path will be put to the top of the list every time you use v(vim) to edit files or o(xdg-open) to open a file from that directory. Or if you like, every time you visit a directory.

You can see it in action here.

| improve this answer | |

You can build your own cd command with pushd, popd, dirs builtin commands.


  • cd -- ( list current history )

  • cd -num ( go to num directory )

  • cd - ( go to previous directory )

function cd()
    local hnum=16  # number of entries
    local new_dir index dir cnt
    if ! [ $# -eq 0 ]; then
        if [[ $# -eq 2 && $1 = "--" ]]; then
            if ! { [ $# -eq 1 ] && [[ $1 =~ ^(-[0-9]{,2}|-|--|[^-].*)$ ]] ;}
                builtin cd "$@"
    [ "$1" = "--" ] && { dirs -v;  return ;}
    if [[ "$new_dir" =~ ^(-[0-9]{,2}|-)$ ]]; then
        [ -z "$index" ] && index=1
        new_dir=$(dirs -l +$index) || return
    pushd -- "$new_dir" > /dev/null || return
    popd -n +$hnum &> /dev/null || true
    new_dir=$PWD cnt=1
    while dir=$(dirs -l +$cnt 2> /dev/null); do
        if [ "$dir" = "$new_dir" ]; then
            popd -n +$cnt > /dev/null
        let cnt++

export -f cd
| improve this answer | |

Well, you could add this code snippet to your ~/.bashrc, which

  1. provides a custom cd command

        function cd ()
            for dir in "${CDHIST[@]}"; do
                [ "$dir" == "$1" ] && {
                    exists == true
            $exists || {
                ${CDHIST[$len]} = "$1"
            builtin cd "$1"
  2. and provides a cd history search command.

    function cdhist ()
        #TODO: Make this magical.
        for dir in "${CDHIST[@]}"; do
            echo "$dir"

Of course, the cdhist command I provided is very basic, and not what you wanted; but it is conceivable to use case statements or parameter expansion to achieve something similar to what you want.

You could even add some "Programmable completion" function, which could be used to add the full cd /path/to/mydir command based on a unique sub-string of /path/to/unique/mydir, although that method would still require you to type cd unique/mydir<tab>.

| improve this answer | |
  • 1
    I misread your code and found bash's dirs command which does essentially the same thing as my_dirs, but on one line. – idbrii Jun 20 '11 at 18:55
  • 1
    Cool. So not only does bash support it; it implements it. I wish I had more time to study bash's deep magic. Of course, this only supports remembering pushd, not cd. But I suppose alias cd=pushd would be the most elegant form. – jpaugh Jun 25 '11 at 0:46

I put this tool together, which combines previous solutions for storing a comprehensive global CLI history with an interactive grepping tool called percol (mapped to C^R). I use it to retrieve commands by where they were run or to find a directory by a command. It's still slick on the first machine I started using it, now with a >2 year old CLI history.

The grep tool works on both path and command, but might still be useful to you. See also 'dirs -v' in zsh


| improve this answer | |
  • this stores a CLI history with path, command and date. It's not path-only, but still useful for reverse searching dir history (bash and zsh) – Gordon Wells Aug 20 '17 at 14:02
  • I think you forgot to link to your tool. From your description, I'd suspect it is deficient in that commands only work from where they're invoked. So cd share/man only works if I'm already in /usr. – idbrii Sep 26 '17 at 16:09
  • oops! my bad, I thought I'd put the url in the text, didn't notice something went wrong – Gordon Wells Oct 27 '17 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.