Home > linux, unix > cd OLD NEW

cd OLD NEW

September 18th, 2009 Leave a comment Go to comments

Something I like in ksh is to change from /my/old/directory/path to /my/new/directory/path by typing cd old new. This does not work in bash

So I had to find a workaround ;)


$ ksh
$ cd /app/oracle/product/11.1.0.6/database/rdbms/admin
$ cd 6 7
$ pwd 
/app/oracle/product/11.1.0.7/database/rdbms/admin
$ bash
$ cd 7 6
bash: cd: 7: No such file or directory
$ c() { cd ${PWD/$1/$2}; }
$ c 7 6
$ pwd
/app/oracle/product/11.1.0.6/database/rdbms/admin

Tags:
  1. September 18th, 2009 at 12:44 | #1

    I’ve also missed this, and your post made me think a bit further… now try this:

    cd() { if [ -n "$2" ]; then builtin cd ${PWD/$1/$2}; else builtin cd $1; fi; }

  2. September 18th, 2009 at 16:42 | #2

    good… then you will also need to take care of the -L or -P parameters

    but I admit I was too coward to alias cd :mrgreen:

  3. September 18th, 2009 at 16:47 | #3

    but still lazy enought to find something shorter (c) :D

  4. September 18th, 2009 at 19:54 | #4

    nice for trick to use “cd” on ksh ;)

    But I use bash shell… perhaps I’ll play about ksh ;)

  5. Danyc
    September 19th, 2009 at 10:39 | #5

    Very nice ! :)

  6. September 19th, 2009 at 22:27 | #6

    @Laurent Schneider do you ever use -L or -P? I didn’t even know these options existed! But even so… easy enough the cover them as well.

    Check the bash manpage for the “builtin” option command… is recommended exactly when you alias cd and the like.

  7. September 19th, 2009 at 23:11 | #7

    cd -L is the default, so no I do not use it. But use cd -P and pwd -P a lot…

    for instance, with 3 params :)

    
    $ cd /sbin
    $ cd -P s /
    $ pwd
    /usr/bin
    

  1. September 19th, 2009 at 15:04 | #1
*