15

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:

/etc/application-2.0.1/options/default

and

/etc/application-1.0.8/options/default

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?

9 Answers 9

12

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

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

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 }
0
9

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}
ing02741@hoster:/home/koushik/Videos$ 

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}
ing02741@hoster:~/src/cdtest/dir-v2.2.2/ind$ 

Borrowing on idea of sepp2k's answer, 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.

1
  • 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, 2017 at 14:32
7

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).

2
  • You only have to press enter the second time if the histverify option of shopt is turned on. Sep 6, 2010 at 20:56
  • @Dennis: thanks, I had forgotten --norc when testing. Sep 6, 2010 at 21:42
3

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
1
  • Btw, tested it in bash and it works there as well. Sep 10, 2010 at 8:18
3
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.

1
  • 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, 2010 at 21:39
3
 ^2.0.1^1.0.8 

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

0
2

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 "$@"
    }
}
1

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.

3
  • 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, 2010 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'. Sep 10, 2010 at 8:22
  • opps, my bad. Sorry I overlooked this. @maxschlepzig - thanks for pointing out.
    – koushik
    Sep 10, 2010 at 11:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .