Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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
up vote 10 down vote accepted

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

share|improve this answer
It might lead to issues with Terminal's title. – Daniel Beck Jun 24 '11 at 11:01

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.

share|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. – renergy Oct 25 '15 at 21:19

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.

share|improve this answer
rock on gearoid – user973810 Jun 30 '15 at 20:47

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

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

share|improve this answer
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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .