3

I have used pushd to move about to various directories and now if I run

dirs -v

I get:

 0  ~/Desktop
 1  /etc
 2  /var/log
 3  ~/Downloads
 4  /tmp

How can I popd to a specific directory in the middle of the stack?, e.g option 2: /var/log

man bash says

+n Removes the nth entry counting from the left of the list shown
by dirs, starting with zero. For example: ``popd +0'' removes the
first directory, ``popd +1'' the second.

I've tried

  • ``popd +0''
  • popd +3

And it pops the correct directory off the stack, but doesn't change the current working directory.

How can I popd the particular directory and change the current working directory to the "popped" dir?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
the_velour_fog
  • 11,840
  • 16
  • 64
  • 109

2 Answers2

3
cd "`dirs +<number>`"

where <number> is 0 or 3 or something else.

In any case, I recommend you check out a cd wrapper such as http://davidcorne.com/tag/cd/ , which pushes onto the dir stack in the background and allows you to do cd -- instead of dirs -v and cd -<number> to get you into the directory you want. It also replaces initial tildas with $HOME, eliminating the problem you've alluded to in the comments.

Petr Skocik
  • 28,176
  • 14
  • 81
  • 141
  • Interesting. The above does work for me. Well, at least it seems you're close as it does try to get you to `/var/log` which, indeed, is the #2-indexed entry in your dir stack. – Petr Skocik Apr 12 '15 at 17:16
  • yes funny, am I supposed to type with the double quotes and back-ticks? if so, do you know why quotes are necessary? – the_velour_fog Apr 12 '15 at 17:19
  • You can experiment with removing the quotes but they should be there for the sake of robustness in case you ever pushd a dir that has a space in its path. – Petr Skocik Apr 12 '15 at 17:21
  • makes sense, My problem seems that when `dirs +` is executed and the value it returned includes a `~` in the path (i.e. any directory in /home) the `cd` command wont accept it. as normally the `~` tilde expansion would be performed prior to the `cd` running – the_velour_fog Apr 12 '15 at 17:30
  • See my edit. If the super cd script doesn't suit you, you can extract the `~` tilda replacement part and put it into a differently named bash function. – Petr Skocik Apr 12 '15 at 17:40
  • thanks, this didnt answer my question directly but pointed me in the right direction enough, when I researched extracting tilda I found (by blind luck) [this article on tilde expansion](http://www.thegeekstuff.com/2010/06/bash-tilde-expansion/) which contained an example for changing dirs. turns out `cd ~+` e.g. `cd ~+2` works nicely when the dir you want to switch to a directory that contains `~` – the_velour_fog Apr 12 '15 at 18:12
0

I found the script in the .bashrc file in the Cygwin distribution. It did not work properly because in line 20

adir=$(dirs +$index)

adir now includes the index, e.g., ' 2 /cygdrive/d' so I added a line thereafter

adir=${adir:4}

and it seems to work

Irwin
  • 1