I either had this somewhere 20 years ago or I dreamed about it.


If if type blobblob I get
blobblob: command not found

Fair enough.

I would like it so that when my shell gets those errors - command not found - it checks to see if a directory exists with that name ('blobblob') and if it does it cd's to that directory.

I'm sure there are some reasons for not doing this or doing it with caution.
I just think it would be pretty neat though and I would like to give it a try by finding how somewhere (like here!).

I have no idea how to do the kinda shell programming this might imply.

  • Bash: shopt -s autocd
  • Zsh: setopt autocd
  • tcsh: set implicitcd

Also, 'autojump' is a useful tool. Once installed it remembers directories so that you can type j abc and if you've visited abc before, say x/d/f/g/t/abc then it will cd to there!

  • 1
    Yup I typed shopt -s autocd at the command line at it worked. Now I can just start typing directory names and use autocomplete and then press return and the shell cd's into the directory. So cool ! – Michael Durrant Apr 24 '12 at 2:23
  • 6
    Great. Thanks for pointing out a misfeature to avoid. – vonbrand Jan 30 '13 at 21:02
  • 3
    I don't use this - cd is not that hard to type, and most tab-completion mechanisms are smarter when you give them more context. – jw013 Jan 30 '13 at 22:44
  • After backspacing an excessive exclamation of appreciation, let me just say that autocd is indeed the right answer :D – Luc May 20 '14 at 18:49
  • The autocd shopt was added in bash version 4. So it is not available on macOS (which still has bash version 3.x.x) unless an alternate, newer bash is installed (e.g., via homebrew). – Quinn Comendant Mar 7 '17 at 10:38

The autojump utility provides a similar mechanism with a Most Heavily Used directory list. It does, however, require the added tedium of typing jSpace but this is compensated by guessing right more often than not.

This is the closest to DWIM that I've used to date.


This also works,

cdf name_of_file_or_directory

..given that you set up a custom cdf.sh script (below) sourced in the shell. For a directory as the parameter, this script only gets to the parent directory for the found directory. Add following line into your .bashrc or .zshrc, whatever..

source ~/bin/cdf.sh 

And add this code into ~/bin/cdf.sh file that you need to create from scratch.


function cdf() {
    echo "cd into directory of ${THEFILE}"
    # For Mac, replace find with mdfind to get it a lot faster. And it does not need args ". -name" part.
    THEDIR=$(find . -name ${THEFILE} |head -1 |grep -Eo "/[ /._A-Za-z0-9\-]+/")
    cd ${THEDIR}
  • Great answer to an old question. I believe you'll receive a badge for reviving an old question, and Welcome to U&L.SE – eyoung100 Sep 10 '14 at 14:08

I once made a useful script for a similar purpose, with a function fcd for find-n-cd.

You type fcd foo and 3 things may happen:

  • No such directory foo is found, then it returns with "no such directory " $1
  • One file is found: It tests whether it is a directory (or just a file) and if so, it cds there.
  • Multiple files are found - then a selection is presented, where you just type the number of the selection (or a special number for return).

It is similar in that it doesn't need you to type the whole path, but you have call the function explicitly.

# find cd. For input foo, find all directories .../.../foo 
# GPLv3 Stefan Wagner (2010, 2012)
# doesn't handle blanks in directory names gracefully.
fcd () 
    list=$(locate $1 | egrep "/$1$")
    count=$(echo $list | wc -w )
    case $count in 
        echo "unknown directory: "$1 && return
        # could search for partial matches Doc => Documentation
        if [[ -d "$list" ]]; then
        echo "$list";
        cd "$list";
            echo "not a directory: $1"
        select directory in $list "/exit/" 
          if [[ "$directory" = "/exit/" ]]; then break; fi
          if [[ -d "$directory" ]]; then
                echo "$directory";
                cd "$directory";
            echo "not a directory: "$1 

You have to source the function (source fcd.sh| . fcd.sh) and can't call it as script, because cd would else only happen in the context of the script, and after finishing you would happen to be back in your starting dir immediately.

Since it works with locate, it is pretty fast in finding directories (but not always up to date).

It doesn't handle blanks in directory names gracefully. If you have an elegant solution for the problem I would be happy.

  • 1
    The question was to cd automatically, without typing any command. This answer is more suitable for Quick directory navigation in the terminal. – manatwork Apr 27 '12 at 11:13
  • 2
    @manatwork: Well, you can combine the command with the error handler of bash. In /etc/bash.bashrc there is, in ubuntu, a function command_not_found_handle which looks as if it could be modified. – user unknown Apr 27 '12 at 15:42
  • Got you. This way sounds much better. – manatwork Apr 27 '12 at 16:15
  • @manatwork: I would do it myself, if I would like to use it myself. But if I want to change directory, I like to use a short command, but if I mistype something, I seldom forgot to type 3 or 4 characters. Having this program step in would be rather annoying to me, but using it explicitly is fine. – user unknown Apr 27 '12 at 21:30

Put this in your ~/.bashrc

  #aliases to cd into the core dirs
  #you can make it search more dirs by assigning new indexes to your other root dirs

    if [ -d "$PARENT_DIR" ]; then
      for PROJECT_DIR in $(/bin/ls $PARENT_DIR); do
        if [ ! -z `which $PROJECT_DIR` ]; then
          continue # don't set alias if there is something already a command on the path with the same name
        if [ -d "$PARENT_DIR/$PROJECT_DIR" ]; then
          alias "$PROJECT_DIR"="cd $PARENT_DIR/$PROJECT_DIR"
  #end alias

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.