Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If you don't know it, I highly recommend learning about CDPATH as well. A good CDPATH means that directories you work in often are always easy to get to. (Add TAB completion in its latest version and they're even easier to get to.)

http://www.caliban.org/bash/ (Under bash Tips and Tricks)

One other good cd trick:

    # Shamelessly stolen from Learning the Bash Shell (3ed), Cameron Newham
    # & Bill Rosenblatt
    my_cd() {
        case "$#" in
            0|1)
                builtin cd $1
        ;;
            2)
                newdir=${PWD//$1/$2}
                case "$newdir" in
                    $PWD)
                        echo "bash: my_cd: bad substitution" >&2
                        return 1
                    ;;
                    *)
                        builtin cd "$newdir"
                    ;;
                esac
            ;;
            *)
                echo "bash: my_cd: wrong arg count" 1>&2
                return 1
            ;;
        esac
    }
The function allows you to do simple search/replace on your current working directory and cd to the replaced version, all in one step. This is a built-in ksh feature, but easy enough to enable in bash. If you have directories with relatively deep nesting and very similar names (e.g. school/2009-2010/compsci1/grades versus school/2010-2011/compsci2/grades), it's a godsend. An example:

    $ pwd
    /Users/telemachus/mmt/2010-2011/compsci2
    $ my_cd compsci2 compsci4
    $ pwd
    /Users/telemachus/mmt/2010-2011/compsci4


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: