The following script came out of a long tradition of writing directory mark and set aliases for persistent storage of working paths.
The original alias that was passed on to me by a fellow co-worker stored directories simply as shell variables, which were both deleted when the shell was exited and also didn't carry over to other shells. I had gotten the idea to store the directory paths as files to overcome both of these limitations.
This program represents an effort to make an easy to install implementation for working on multiple systems as it handles directory creation automatically and is contained in a single file. The following code needs to be sourced instead of ran as the only way to create something large and complex like this that can change the working directory of the calling shell is to either write a program or create functions (there are some other really ugly ways, but I like this one best.)
In the near future, I may find myself writing some compiled code to do the same thing, which will give the advantage of not needing to be sourced and also working in other shells aside from 'sh' derived ones.
I leave it to the reader to learn the usage of the program from the source (just read through 'mrk_printhelp'.)
#!/bin/sh
# These two functions must be sourced so they can be used from the command
# prompt. Sourcing these functions as opposed to writing a script must be
# done due to the fact that a subshell can't change the directory of the
# calling process, which would make 'mrk goto [NAME]' useless.
#
# To install this, put this file somewhere and source the file before use.
# You may want to put it in .bash_profile or .profile (whichever one you're
# using.)
function mrk_printhelp () {
echo "Usage: mrk [COMMAND [NAME]]"
echo " Directory marker. Marks the current directory by name and persistently"
echo " stores that name in ~/.mrk for later retrieval. Useful for jumping around"
echo " multiple directories with frequency. Can store any number of directories."
echo ""
echo "Options:"
echo " NAME:"
echo " The name assigned to a directory location."
echo ""
echo " COMMAND:"
echo " set - Saves current path as NAME"
echo " list - List all names currently stored"
echo " goto - Goto directory saved under NAME"
echo " del - Delete the directory stored under NAME"
echo " help - This help message"
echo ""
echo "Examples:"
echo " /some/really/long/path$ mrk set pth # store long path as 'pth'"
echo ""
echo " ~$ mrk list # list dirs"
echo " pth => /some/really/long/path"
echo ""
echo " ~$ mrk goto p1 # goto 'pth' from home dir"
echo " /some/really/long/path$"
echo ""
echo " /some/really/long/path$ mrk del pth # delete 'pth'"
echo ""
echo " /some/really/long/path$ mrk list # 'pth' is gone"
echo ""
}
function mrk {
# First make sure the mrk directory exists.
# if not, create it
if [ ! -d ~/.mrk ]; then
if [ -e ~/.mrk ]; then
echo 'Error, file exists but is not a directory.' 1>&2
return
fi
echo 'First time user. Creating ~/.mrk'
mkdir -p ~/.mrk
fi
# Check to make sure we have either 1 or 2 args. If not,
# print help and break
if [ $# -eq 0 -o $# -gt 2 ] ; then
mrk_printhelp
return
fi
# This handles all 2 argument cases
if [ $# -eq 2 ]; then
case $1 in
('set')
echo `pwd` > ~/.mrk/mrk_$2
return;;
('goto')
cd `cat ~/.mrk/mrk_$2`
return;;
('del')
rm -f ~/.mrk/mrk_$2
return;;
esac
fi
# The one argument cases
if [ $# -eq 1 ]; then
case $1 in
('list')
for v in `ls ~/.mrk/`; do
u=`echo $v | sed 's@^mrk_@@'`
echo "$u => `cat ~/.mrk/$v`"
done
return;;
('help')
mrk_printhelp;
return;;
esac
fi
# If we get here for some reason, we need to print the help
mrk_printhelp;
}