cd by just typing the directory's name?

5,827

Solution 1

  • 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

Solution 2

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.

Solution 3

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.

#!/bin/bash

function cdf() {
    THEFILE=$1
    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}
}

Solution 4

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:
5,827

Related videos on Youtube

Michael Durrant
Author by

Michael Durrant

rails ruby rspec rock

Updated on September 18, 2022

Comments

  • Michael Durrant
    Michael Durrant over 1 year

    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.

  • Michael Durrant
    Michael Durrant about 12 years
    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 !
  • user unknown
    user unknown about 12 years
    @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.
  • manatwork
    manatwork about 12 years
    Got you. This way sounds much better.
  • user unknown
    user unknown about 12 years
    @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.
  • vonbrand
    vonbrand over 11 years
    Great. Thanks for pointing out a misfeature to avoid.
  • jw013
    jw013 over 11 years
    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.
  • Luc
    Luc almost 10 years
    After backspacing an excessive exclamation of appreciation, let me just say that autocd is indeed the right answer :D
  • eyoung100
    eyoung100 over 9 years
    Great answer to an old question. I believe you'll receive a badge for reviving an old question, and Welcome to U&L.SE
  • Quinn Comendant
    Quinn Comendant about 7 years
    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).