Changing my login directory

Karl Vogel vogelke+unix at
Fri Jun 19 19:07:58 UTC 2009

>> On Wed, 17 Jun 2009 21:13:32 -0400, 
>> Steve Bertrand <steve at> said:

S> I've got a couple of jails now that I use exclusively for Perl
S> development.  As soon as I log into the box via SSH, my first command is
S> _always_ "cd devel/something".  I'd like to make it so that '~' remains
S> /home/steve, but when I log in, I would prefer to be dropped immediately
S> into /home/steve/devel.

   This reminded me of how I deal with lots of directories.  If you
   have a project with files spread all over the place, here's a way to
   navigate between 20-25 directories with no more than 3-4 keystrokes.
   First, install the "grabchars" program:

   It's very useful for capturing and validating keystrokes from within
   a shell script.  For example:
     ans=`grabchars -q'Answer y or n: '`

   will print "Answer y or n: ", and it will store your reply after
   pressing just one key, no need to hit return.  Combine this with a
   function to change your current directory and you're in business;
   it has to be done in a shell function rather than a separate script
   because it modifies your current shell working directory.

   Second, create a file holding the directories you use the most:

     me% cat $HOME/.cdlist
     0   /home/vogelke
     1   /home/vogelke/today
     2   /home/vogelke/notebook/2007/0414/new-homepage
     8   /doc/html/htdocs/blog/posts
     9   /doc/sitelog/server1
     a   /doc/sitelog/server2
     b   /doc/sitelog/server3
     o   /home/vogelke/src

   The first field is any digit, and the lowercase letters a-o.  You can
   use more letters, but I've found 25 choices to be more than sufficient.
   The second field is the full path to the directory.

   Finally, make a function using "grabchars" that will display this list,
   prompt for a single character, and immediately change to that directory.
   I use "jd" for the function name, but if you're not using the letter 'j'
   for any commands, you can shorten this to three keystrokes.

   Here's the function setup for the Korn and bash shells:

     # Jump to a directory.
     jd () {
         cat $HOME/.cdlist
         local prompt="dir: "
         local ans=`grabchars -d0 -L -c '[0-9a-z]' -q"$prompt"`
         set X `grep "^$ans" $HOME/.cdlist`
         case "$#" in
             (3) cd $3; echo; echo "pwd: `/bin/pwd`" ;;
             (*) echo no such entry ;;

     # Dump the PS1 stuff if you don't want to change your prompt.
     chdir () {
         command cd "$@"
         /bin/pwd >> $HOME/
         PS1="${CWDH##*/}/${PWD##*/} ->"
         export PS1
     alias cd=chdir

   Here's the function setup for the Z-shell:

     # Jump to a directory.
     jd () {
         cat $HOME/.cdlist
         local prompt="dir: "
         local ans=`grabchars -d0 -L -c '[0-9a-z]' -q"$prompt"`
         set X `grep "^$ans" $HOME/.cdlist`
         case "$#" in
             (3) chdir $3 && echo && echo "pwd: `/bin/pwd`" ;;
             (*) echo no such entry ;;

     # This function is run after you change directories.
     chpwd () {
         /bin/pwd >> ~/

   To find the most often-used directories, the "chdir" and "chpwd"
   functions above will store the name of every directory you cd to in
   "$HOME/".  To find your most popular directories:

     me% sort ~/ | uniq -c | sort -n | tail

Karl Vogel                      I don't speak for the USAF or my company

I don't want to achieve immortality through my work; I want to achieve
immortality through not dying.                          --Woody Allen

More information about the freebsd-questions mailing list