removing or clearing stack of popd/pushd paths

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.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

dirs -c is what you are looking for.

Method 2

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

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments