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.
dirs -c is what you are looking for.
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
-loption here will list full paths, to make sure we don’t fail if we try to
~, and the
-0retrieves the first entry from the stack bottom.
@jw013 suggested making this command more robust, by avoiding path expansions:
pushd -0 && dirs -c
popduntil you encounter an error (which is the status of a
popdcall when the directory stack is empty):
while (( $? == 0 )); do popd; done