Not sure if it is possible, but i seem to remember from my old days using AIX it was possible to change my path by just saying which part of the path needed to be replaced by something else. For instance, say i have two paths:




that i could switch from folder 1 to 2 using a command like

cd /2.0.1/1.0.8/

which would replace, in the path, the string 2.0.1 to 1.0.8. Obviously this does not work for me now. But is there a way to do this?


If you use zsh as shell you can just enter cd 1.0.8 2.0.1.

| improve this answer | |
  • This seems the best option to me. Now looking how to install that thing :) – nathanvda Sep 10 '10 at 7:43
  • 1
    sudo apt-get install zsh – koushik Sep 10 '10 at 11:47
  • 1
    RandomMonkey's answer allows the full path substitution functionality within Bash's "cd" built-in. – kmarsh Jan 12 '17 at 14:31

This should work in bash on ubuntu 10.04 : cd ${PWD/old/new}. Basically this replaces first occurrence of old in your present working directory with new. 2 examples below.

Example 1

ing02741@hoster:~$ cd /home/ing02741/Videos/
ing02741@hoster:~/Videos$ cd ${PWD/ing02741/koushik}

Example 2

ing02741@hoster:~/src/cdtest$ mkdir dir-v1.0.1 dir-v2.2.2 dir-v3.0.7
ing02741@hoster:~/src/cdtest$ mkdir dir-v1.0.1/ind dir-v2.2.2/ind dir-v3.0.7/ind
ing02741@hoster:~/src/cdtest$ cd dir-v1.0.1/ind/
ing02741@hoster:~/src/cdtest/dir-v1.0.1/ind$ cd ${PWD/1.0.1/2.2.2}

Borrowing on idea of sepp2k above, you could make a function like this

function mycd { cd ${PWD/$1/$2} }

and then use something like mycd 2.0.1 1.0.8 to switch.

| improve this answer | |
  • 1
    This is 1/2 way there. If you look at RandomMonkey's answer, you'll see full functionality is possible within the Bash Cd built-in. – kmarsh Jan 12 '17 at 14:32

I have used (and missed) this feature myself. It depends on which flavor and/or release of *nix you are using. If you use bash, here is a handy way to extend the builtin cd to include this functionality. Put this in your .bashrc (or to test paste it in your bash shell and hit enter).

function cd() { if [ $# -eq 2 ]; then builtin cd "${PWD/$1/$2}"; else builtin cd "$1"; fi }
| improve this answer | |

You're probably remembering history expansion. I don't know what was available in your shell on AIX, but one way to do this in bash is ^2.0.1^1.0.8.

History expansion is less useful with shells like bash and zsh that have powerful command line editing. You can use arrow keys to recall previous commands, and Alt+. to insert the last word of the previous command (press it twice to reach the command before that and so on).

| improve this answer | |

If you are a vi fan you could enable the vi mode in your shell (bash set -o vi for example) and use the command mode of vi ...

Or you could do crazy history expansion (tested in zsh, perhaps in bash as well):

$ cd /etc/application-1.0.8/options/default
cd: no such file or directory: /etc/application-1.0.8/options/default

$ !!:s/1.0.8/2.0.1/
cd /etc/application-2.0.1/options/default
cd: no such file or directory: /etc/application-2.0.1/options/default
| improve this answer | |
  • Btw, tested it in bash and it works there as well. – maxschlepzig Sep 10 '10 at 8:18
cd `pwd | sed 's/2\.0\.1/1\.0\.8/'`

However this isn't very pretty. You can pretty it up a bit, by putting it in a function:

function mycd { cd `pwd | sed s$1` }

And then calling it like:

mycd '/2\.0\.1/1.0.8/'

You still need to escape the dots, though, but I'm sure this can be worked around as well with a bit of thought.

| improve this answer | |
  • You certainly don't need to escape the dots on the right side of the substitution. In most cases, not escaping the dots on the left will be harmless too, unless you've got a lot of similarly named folders. – frabjous Sep 6 '10 at 21:39

The above command swap 2.0.1 to 1.0.8 in last command, its work in bash

| improve this answer | |

To ensure that -P|-L|- work use the following:

function cd () {
    typeset arg=
    case $1 in -|-L|-P) arg=$1 ; shift ;; esac
    [ $# -gt 1 ] && {
        builtin cd $arg "${PWD/$1/$2}"
    } || {
        builtin cd $arg "$@"
| improve this answer | |

Not sure about AIX, but I remember this as an old Korn shell trick on the Solaris boxes I used to administer. In your example, you'd type in the command:

cd 2.0.1 1.0.8

Some more info here.

| improve this answer | |
  • This didn't work for me: ubuntu 10.04, bash "GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)", I tried following commands : 2048 cd 2049 cd ~/src/local-ubuntu-patches/cdtest/dir-v1.0.9/gnome-control-center-2.30.1/capplets/network/ 2050 cd dir-v1.2.9 dir-v1.0.9 /home/ing02741/src/local-ubuntu-patches/cdtest/dir-v1.2.9/gnome-control-center-2.30.1/capplets/network is a valid path. – koushik Sep 7 '10 at 12:52
  • @koushik: He wrote that it is a Korn shell trick. You are using bash. bash != korn. Under Ubuntu ksh is available via the package 'ksh'. – maxschlepzig Sep 10 '10 at 8:22
  • opps, my bad. Sorry I overlooked this. @maxschlepzig - thanks for pointing out. – koushik Sep 10 '10 at 11:45

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.