58

After pushding too many times, I want to clear the whole stack of paths.

How would I popd all the items in the stack?

I'd like to popd without needing to know how many are in the stack?

The bash manual doesn't seem to cover this.

Why do I need to know this? I'm fastidious and to clean out the stack.

chrisjlee
  • 8,283
  • 16
  • 49
  • 54
  • 1
    BTW, the [complete bash manual](http://www.gnu.org/software/bash/manual/) is over at gnu.org. If you use the all on one page version, it may be easier to find stuff there. – jw013 Feb 09 '12 at 06:39
  • Ah wish google indexed that one. Thanks @jw013 – chrisjlee Feb 09 '12 at 07:14

2 Answers2

62

dirs -c is what you are looking for.

jw013
  • 50,274
  • 9
  • 137
  • 141
11

In order to both empty the stack and restore the working directory from the stack bottom, either:

  • retrieve that directory from dirs, change to that directory, and than clear the stack:

    cd "$(dirs -l -0)" && dirs -c
    

    The -l option here will list full paths, to make sure we don't fail if we try to cd into ~, and the -0 retrieves the first entry from the stack bottom.

    @jw013 suggested making this command more robust, by avoiding path expansions:

    pushd -0 && dirs -c
    
  • or, popd until you encounter an error (which is the status of a popd call when the directory stack is empty):

    while (( $? == 0 )); do popd; done
    
Eliran Malka
  • 259
  • 2
  • 10
  • 1
    The first method is exactly what I wanted. The second wouldn't work in my case since I had called `pushd` a few times, then removed one of the directories in the middle, then `popd` was failing when I tried to unroll. I needed to jump over all the buggered up stuff in the middle to get back to where I started. – Chuck Wilbur Nov 14 '17 at 18:21
  • right @ChuckWilbur - if you scrambled the dir stack, `popd` won't save you :) – Eliran Malka Nov 14 '17 at 22:51
  • 1
    It's better to `pushd -0` instead of `cd "$(dirs ...)"`. – jw013 Dec 07 '17 at 20:50
  • @jw013 how so? that would mess with the dir stack even more (which we're trying to clear here..) – Eliran Malka Dec 11 '17 at 13:56
  • The last step is to clear it (`dirs -c`) so you are left with an empty stack, so it does not matter unless I misunderstood your question. – jw013 Dec 11 '17 at 14:45
  • fair enough, just asking why is that necessary, or, how is that "better"? – Eliran Malka Dec 11 '17 at 15:00
  • 5
    `cd "$(...)"` works in 90%, probably even 99% of use cases, but with `pushd -0` you can confidently say 100%. There are so many potential gotchas and edge cases associated with expanding file/directory paths in the shell that the most robust thing to do is just avoid it altogether, which `pushd -0` does very concisely. There is no chance of getting caught by a bug with a weird edge case if you never take the risk. If you want further reading on the possible headaches involved with Unix file / path names, a good starting point is http://mywiki.wooledge.org/ParsingLs – jw013 Dec 12 '17 at 15:31
  • awesome! thanx for elaborating – Eliran Malka Dec 12 '17 at 23:45