2

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?

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.

  • 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. – PSkocik 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. – PSkocik Apr 12 '15 at 17:21
  • makes sense, My problem seems that when dirs +<number> 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. – PSkocik Apr 12 '15 at 17:40
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

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.