Path: utzoo!attcan!uunet!lll-winken!ames!ncar!noao!asuvax!nud!df From: df@nud.UUCP (Dale Farnsworth) Newsgroups: comp.unix.questions Subject: Re: KSH question about past directories Summary: My ksh functions to keep a directory history Message-ID: <10034@nud.UUCP> Date: 24 Jan 89 22:07:32 GMT References: <821@optilink.UUCP> Reply-To: df@nud.UUCP (Dale Farnsworth) Organization: Motorola Microcomputer Division, Tempe, Az. Lines: 123 Sumit Dongre (dongre@optilink.UUCP) writes: > Is there a way to track/recover the last few directories in ksh-i. No, but I added three options to cd via ksh functions. 1. cd -l Displays the most recently used 20 current directories. 2. cd -foostring Changes directory to the most recent directory whose name contains the string foostring. 3. cd -8 Changes to the eighth most recent directory. The directory list is maintained in most recently used order. Here are the functions: # If you want multiple shells to share the same history (either # concurrently or serially, create a file and place its name in # the shell variable CDHISTFILE. alias cd=_cd function _cd { typeset -i cdlen i status typeset t if [ $# -eq 0 ] then set -- $HOME fi t="$@" if [ "$CDHISTFILE" -a -r "$CDHISTFILE" ] then typeset CDHIST= set -- `<$CDHISTFILE` ((i=-1)) while [ $# -gt 0 ] do CDHIST[i=i+1]=$1 shift done fi set -- $t if [ "${CDHIST[0]}" != "$PWD" -a "$PWD" != "" ] then _cdins fi cdlen=${#CDHIST[*]} case "$@" in -) if [ "$OLDPWD" = "" ] && ((cdlen>1)) then print ${CDHIST[1]} 'cd' ${CDHIST[1]} ; status=$? else 'cd' $@ ; status=$? fi ;; -l) typeset -R3 num ((i=cdlen)) while (((i=i-1)>=0)) do ((num=i)) print "$num ${CDHIST[i]}" done return 0 ;; -[0-9]|-[0-9][0-9]) if (((i=${1#-})=cdlen)) ; then 'cd' $@ ; status=$? fi ;; *) 'cd' $@ ; status=$? ;; esac _cdins if [ "$CDHISTFILE" ] then print -r ${CDHIST[*]} >$CDHISTFILE fi # set_prompt return $status } function _cdins { typeset -i i ((i=-1)) while (((i=i+1)<${#CDHIST[*]})) do if [ "${CDHIST[$i]}" = "$PWD" ] then break fi done if ((i>20)) then ((i=20)) fi while (((i=i-1)>=0)) do CDHIST[i+1]=${CDHIST[i]} done CDHIST[0]=$PWD } -Dale -- Dale Farnsworth 602-438-3092 noao!asuvax!nud!df