Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

Basically:

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.

share|improve this question

4 Answers 4

up vote 68 down vote accepted
  • 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!
https://github.com/joelthelion/autojump

share|improve this answer
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
4  
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 at 18:49

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.

share|improve this answer

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.

#!/bin/bash
#
# 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 
      0) 
        echo "unknown directory: "$1 && return
        # could search for partial matches Doc => Documentation
        ;;
      1) 
        if [[ -d "$list" ]]; then
        echo "$list";
        cd "$list";
        else
            echo "not a directory: $1"
        fi
        ;;
      *) 
        select directory in $list "/exit/" 
        do
          if [[ "$directory" = "/exit/" ]]; then break; fi
          if [[ -d "$directory" ]]; then
                echo "$directory";
                cd "$directory";
            break
          else
            echo "not a directory: "$1 
          fi
        done     
        ;;
   esac
}

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.

share|improve this answer
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
  PROJECT_PARENT_DIRS[0]="$HOME/repos"
  #you can make it search more dirs by assigning new indexes to your other root dirs

  for PARENT_DIR in ${PROJECT_PARENT_DIRS[@]} ; do
    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
        fi
        if [ -d "$PARENT_DIR/$PROJECT_DIR" ]; then
          alias "$PROJECT_DIR"="cd $PARENT_DIR/$PROJECT_DIR"
        fi
      done
    fi
  done
  #end alias
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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