Q: 'push' and 'pop' for changing directories?

Q: 'push' and 'pop' for changing directories?

Post by Bobby D. Bryan » Thu, 12 Nov 1998 04:00:00



Back when I worked in a VMS shop there was a utility that manipulated
environment variables to let you impose a stack discipline on your
directory changes, so that you could repeatedly type "push /whatever" to
change to another directory, and then repeatedly type "pop" to cd back
to the previous directories.  It saved a lot of typing.

A Web search doesn't turn up any such thing for Linux.  Do you know
where such a thing can be found?

Bobby Bryant
Austin, Texas

 
 
 

Q: 'push' and 'pop' for changing directories?

Post by Chris J/# » Thu, 12 Nov 1998 04:00:00


Under pdksh, I set up some funtions to do the job...as this is rather
large, I suggest pasting it into an alternative file and source it
from .profile or your $ENV file (eg, . $HOME/.ksh_funcs -- don't forget
the leading '.' in front of the $HOME).

This is a ksh-only solution as I'm using its arithmatic features...it
was a quick hack for use at work - I don't claim its elegant or simple :)

Chris...
ps, functions listed below.



Quote:>Back when I worked in a VMS shop there was a utility that manipulated
>environment variables to let you impose a stack discipline on your
>directory changes, so that you could repeatedly type "push /whatever" to
>change to another directory, and then repeatedly type "pop" to cd back
>to the previous directories.  It saved a lot of typing.

>A Web search doesn't turn up any such thing for Linux.  Do you know
>where such a thing can be found?

>Bobby Bryant
>Austin, Texas

#############################################################################
## .ksh_funcs -- source from $ENV or .profile
#############################################################################
##
## Fun functions definitions for all the family
## Ages 7 and up :-)
##
#############################################################################

function dir_stack_setup
{
        cdstack=${HOME}/.cdstack

Quote:}

function push
{
        dir_stack_setup
        if [[ ! -f $cdstack ]]
        then
                touch $cdstack
                chmod 600 $cdstack
        fi
        pwd >> $cdstack

Quote:}

function pop
{
        dir_stack_setup
        if [[ ! -f $cdstack ]]
        then
                echo "Cannot pop from cd stack...stack is empty"
        else
                mv $cdstack ${cdstack}.old
                olddir=`tail -1 ${cdstack}.old`
                lines=`cat ${cdstack}.old | wc -l | sed 's/ *//g'`
                (( lines=lines-1 ))
                if [[ $lines -eq 0 ]]
                then
                        rm -f ${cdstack}.old
                else
                        cat ${cdstack}.old | head -$lines > $cdstack
                        rm -f ${cdstack}.old
                fi
                cd $olddir
                pwd
                unset olddir
                unset lines
        fi

Quote:}

function getdir
{
        dir_stack_setup
        if [[ ! -f $cdstack ]]
        then
                echo "Stack is empty."
        else
                if [[ $# -ne 1 ]]
                then
                        echo "Get which directory number?"
                else
                        dir=`grep -n '.' $cdstack | grep "^$1\:" | sed 's/^[0-9]*\://'`
                        if [[ x$dir = x ]]
                        then
                                echo "Directory number does not exist!"
                        else
                                cd $dir
                                echo "`pwd` (not removed)"
                        fi
                        unset dir
                fi
        fi

Quote:}

function liststack
{
        dir_stack_setup
        if [[ ! -f $cdstack ]]
        then
                echo "Stack is empty."
        else
                lines=`cat $cdstack | wc -l | sed 's/ *//g'`
                if [[ $lines -eq 0 ]]
                then
                        echo "Stack is empty."
                else
                        echo "Directory stack (item 1 is bottom of stack):"
                        grep -n '.' $cdstack
                fi
                unset lines
        fi

Quote:}

function delstack
{
        dir_stack_setup
        if [[ ! -f $cdstack ]]
        then
                echo "Stack is already empty."
        else
                liststack
                echo ""
                print -n "Delete the stack (y/n)? "
                read reply
                if [[ ( x$reply = xy ) || ( x$reply = xY ) ]]
                then
                        rm -f $cdstack
                        echo "Stack cleared."
                else
                        echo "Delete aborted."
                fi
                unset reply
        fi

Quote:}

--

   \ If not for me then do it for yourself  \ www.nccnet.co.uk/~sixie/   \
    \ If not for me then do it for the world \ pine.shu.ac.uk/~cjohnso0/  \
     \                   -- Stevie Nicks      \                            \

 
 
 

Q: 'push' and 'pop' for changing directories?

Post by NF Steve » Thu, 12 Nov 1998 04:00:00



Quote:>Back when I worked in a VMS shop there was a utility that manipulated
>environment variables to let you impose a stack discipline on your
>directory changes, so that you could repeatedly type "push /whatever" to
>change to another directory, and then repeatedly type "pop" to cd back
>to the previous directories.  It saved a lot of typing.

It's available (as pushd and popd) in the bash shell (which is available
on all major distros)

Norman

 
 
 

Q: 'push' and 'pop' for changing directories?

Post by Frank Van den Heuve » Fri, 13 Nov 1998 04:00:00



> Back when I worked in a VMS shop there was a utility that manipulated
> environment variables to let you impose a stack discipline on your
> directory changes, so that you could repeatedly type "push /whatever" to
> change to another directory, and then repeatedly type "pop" to cd back
> to the previous directories.  It saved a lot of typing.

> A Web search doesn't turn up any such thing for Linux.  Do you know
> where such a thing can be found?

> Bobby Bryant
> Austin, Texas

Most shell languages and (bash and tcsh in particular) have the pushd,
popd
command pair with which you can perform all of these functions. It is
handy to have some aliases defined as "pd2 pushd +2" to work more
efficiently
but it's all there I also implemented :
back set back =$old; set old=$cwd;cd $back; unset back;dir
this allows you to go back to the previous directory without the
stacking option.

Pointers like this can be readily found in a book : Unix Power Tools
is a treasure for such things. (a Nutshell book distributed by
O'Reilly).

Hope this helps

Frank

--
______________________________________________________________________________

ROC Harper Hospital                       Voice: (313) 745 24 66
Karmanos Cancer Institute                 Fax: (313) 745 23 14
Wayne State University                    
3990 John R., Detroit, MI 48201

  vcard.vcf
< 1K Download
 
 
 

Q: 'push' and 'pop' for changing directories?

Post by James Youngm » Fri, 13 Nov 1998 04:00:00



Quote:> Back when I worked in a VMS shop there was a utility that manipulated
> environment variables to let you impose a stack discipline on your
> directory changes, so that you could repeatedly type "push /whatever" to
> change to another directory, and then repeatedly type "pop" to cd back
> to the previous directories.  It saved a lot of typing.

I tend to use
        $ cd /wherever
end then
        $ cd -
to go back.

--