ncd

Name Directory/NCD

And now for a little tech! One of the first things I wrote about 20 years ago when I started using Unix was a way to "name" directories so I could cd back to them easily. Something like:

% cd /usr/local/bin

% nd ulb

% cd ~

% ncd ulb

% pwd

/usr/local/bin

Anyway, I decided to update these for the BASH shell and learned a few weird things about aliases and functions that I didn't know--including that there is no longer a chdir command (from my csh days) so you need to be careful with aliasing cd (use \cd instead inside the function) -- if you don't you will find that cd can hang your shell!

# Name Directory functions

function nd { eval "export ND_$1=\`/bin/pwd\`" ; }

function ncd

{

if [ ! -n "$1" ]; then

\cd

elif echo "$1" | grep --silent / ; then

#echo Found a slash using regular CD

\cd $1

else

NDSTRING=\$ND_$1

eval "target=$NDSTRING"

#echo NDSTRING is $NDSTRING

#echo target is [$target]

if [ "$target"x == ""x ]; then

#echo No Named CD so CDing to $1

\cd $1

elif [ ! -d "$target" ]; then

#echo Named CD not a directory so CDing to $1

\cd $1

else

#echo Named CD to $target

\cd $target

fi

fi

}

alias cd='ncd'