Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

8 Answers 8

up vote 7 down vote accepted

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

share|improve this answer
    
This seems the best option to me. Now looking how to install that thing :) –  nathanvda Sep 10 '10 at 7:43
    
sudo apt-get install zsh –  koushik Sep 10 '10 at 11:47

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

share|improve this answer
    
You only have to press enter the second time if the histverify option of shopt is turned on. –  Dennis Williamson Sep 6 '10 at 20:56
    
@Dennis: thanks, I had forgotten --norc when testing. –  Gilles Sep 6 '10 at 21:42

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

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

share|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
 ^2.0.1^1.0.8 

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

share|improve this answer
    
+1, I can't believe no one remembered this! –  dr Hannibal Lecter Oct 13 '10 at 17:50

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 }
share|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.

share|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/capplet‌​s/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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.