• linux bash
  • pushd/popd/dirs


The problem scenario is very similar to the one stated in this question: removing or clearing stack of popd/pushd paths ... however the goal is not to clear the stack, but rather to prune it. Specifically, the pruning operation is to remove duplicates.


Is there a straightforward way to prune the output of dirs -v -p such that there are no duplicates in the stack?

  • 2
    In zsh, setopt pushd_ignore_dups makes this systematic. – Gilles Jun 8 '16 at 22:34
  • 1
    @Gilles: yeah, all the cool kids keep telling me to use zsh ... – dreftymac Jun 8 '16 at 22:41

This function should remove dups.

    declare -a new=() copy=("${DIRSTACK[@]:1}")
    declare -A seen
    local v i
    for v in "${copy[@]}"
    do if [ -z "${seen[$v]}" ]
       then new+=("$v")
    dirs -c
    for ((i=${#new[@]}-1; i>=0; i--))
    do      builtin pushd -n "${new[i]}" >/dev/null

It copies the list of dirs, except the first which is bogus, into an array copy, and for each dir adds it to a new array if we have not already seen it (an associative array). This ensures older dup entries, which are later in the array, are not copied. The dir list is cleared, and the array is then pushd in reverse order.

The first bogus element of the dirs list is the current directory, which is unchanged. It is set in the seen array at the start to also get it removed if earlier in the dir list.

If you want to do this automatically, you can override pushd eg:

    builtin pushd "$@"

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.