removing or clearing stack of popd/pushd paths

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
    

    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
    

Tags:

Bash

Pushd