Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!sri-spam!sri-unix!hplabs!hao!noao!mcdsun!nud!df From: df@nud.UUCP Newsgroups: net.sources,comp.unix.questions,comp.unix.wizards Subject: Korn Shell LRU Directory History Message-ID: <337@nud.UUCP> Date: Thu, 26-Mar-87 00:25:33 EST Article-I.D.: nud.337 Posted: Thu Mar 26 00:25:33 1987 Date-Received: Sat, 28-Mar-87 08:36:41 EST Reply-To: df@nud.UUCP (Dale Farnsworth, NO7K) Organization: Motorola Microcomputer Division, Tempe, Az. Lines: 188 Keywords: Korn shell ksh aliases directory history Xref: utgpu net.sources:6409 comp.unix.questions:1519 comp.unix.wizards:1555 With the recent discussion on Korn Shell aliases for cd, I thought I'd post my own cd alias. It maintains a directory history in least recently used (LRU) order. You can print the directory history and cd to a recent directory via number or pattern matching. I much prefer it to pushd/popd since the history occurs automatically through the cd command. Optionally, all your shells can share the same directory via a history file, though there is a slight performance hit for this particular feature. Here is an example of its use. ============================================================== $ pwd /usr/df $ cd / $ cd /bin $ cd /usr/bin $ cd -l # "cd -l" lists directory history 3 /usr/df 2 / 1 /bin 0 /usr/bin $ cd -3 # "cd -3" cd's to third most recent directory /usr/df $ cd -l 3 / 2 /bin 1 /usr/bin 0 /usr/df $ cd -bin # "cd -foo" cd's to most recent directory name /usr/bin # containing the string "foo" $ cd -l 3 / 2 /bin 1 /usr/df 0 /usr/bin $ ============================================================== From $HOME/.profile: ============================================================== export CDHISTFILE CDHIST PWD CDHIST=$HOME # CDHISTFILE=$HOME/.cdhistory # set this for directory history file. CDHISTFILE= ENV=$HOME/.kshrc ============================================================== From $HOME/.kshrc (Korn shell ENV file): ============================================================== alias set_prompt="PS1='$ '" # of course, my real prompt is different. alias cd='. $HOME/.functions; _cd' alias md='. $HOME/.functions; md' ============================================================== From $HOME/.functions (Where my function definitions reside): ============================================================== alias cd=_cd function _cd { typeset -i cdlen i typeset t if [ $# -eq 0 ] then set -- $HOME fi if [ "$CDHISTFILE" -a -r "$CDHISTFILE" ] # if directory history exists then typeset CDHIST i=-1 while read -r t # read directory history file do CDHIST[i=i+1]=$t done <$CDHISTFILE fi if [ "${CDHIST[0]}" != "$PWD" -a "$PWD" != "" ] then _cdins # insert $PWD into cd history fi cdlen=${#CDHIST[*]} # number of elements in history case "$@" in -) # cd to new dir if [ "$OLDPWD" = "" ] && ((cdlen>1)) then print ${CDHIST[1]} 'cd' ${CDHIST[1]} else 'cd' $@ fi ;; -l) # print directory list typeset -R3 num ((i=cdlen)) while (((i=i-1)>=0)) do num=$i print "$num ${CDHIST[i]}" done return ;; -[0-9]|-[0-9][0-9]) # cd to dir in list if (((i=${1#-})=cdlen)) then 'cd' $@ fi ;; *) # cd to new dir 'cd' $@ ;; esac _cdins # insert $PWD into cd history if [ "$CDHISTFILE" ] then cdlen=${#CDHIST[*]} # number of elements in history i=0 while ((i$CDHISTFILE fi set_prompt } function _cdins # insert $PWD into cd history { # meant to be called only by _cd typeset -i i ((i=0)) while ((i<${#CDHIST[*]})) # see if dir is already in list do if [ "${CDHIST[$i]}" = "$PWD" ] then break fi ((i=i+1)) done if ((i>22)) # limit max size of list then i=22 fi while (((i=i-1)>=0)) # bump old dirs in list do CDHIST[i+1]=${CDHIST[i]} done CDHIST[0]=$PWD # insert new directory in list } ============================================================== Cd is a sizable alias, but actually executes quite quickly. I hope it is useful to someone else; I've been using it for several months. -Dale -- seismo!noao!mcdsun!nud!df 602/438-5739 ihnp4!mot!nud!df