[Discuss] efficiently changing directories?

Daniel M German dmgerman at uvic.ca
Mon Aug 13 13:32:57 PDT 2007


Here is an improved version that updates the CDPATH with the new
directory if it is not there already. Useful for a directory that
might be useful in this session, but not worth putting there all the
time.

Alias:

pcd() { eval $(pcd.sh "$1"); }


Code:

----------------------------------------------------------------------=
#! /bin/sh
# NAME
# 
#  pcd.sh - look for a directory by prepending paths in CD_PATH
# 
# SYNOPSYS
# 
#  # in your ~/.profile
#  # set a list of commonly-used directories to search
#  export CD_PATH=~/my_projects:my_other_directory
#  # make a function to use pcd.sh to find a directory to cd to using pcd.sh
#  pcd() { cd $(pcd.sh "$1"); }
# 
#  # on the command-line
#  $ pcd mydirectory
# 
# 
# DESCRIPTION
# 
# This echoes the first path that is a directory by checking:
# 
#    1. its first argument itself 
# 
#    2. its first argument with a directory from the CD_PATH environment
#       variable prepended
# 
# The idea is to make a shell utility that you can use to jump to
# subdirectories in commonly-used directories.
# 
# AUTHOR
# 
# Noel Burton-Krahn <noel at burton-krahn.com>
# Aug 8, 2007
#
# 

dir="$1"

IFS=":;"
for p in $CDPATH; do
    if [ "$p" ]; then
	t="$p/$dir"
    else
	t="$dir"
    fi

    if [ -d "$t" ]; then 
	echo "cd $t"
	exit 0
    fi
done
export CDPATH1=$CDPATH:$1
echo "CDPATH=$CDPATH:$1;cd $dir"



-- 
--
Daniel M. German                  
http://turingmachine.org/
http://silvernegative.com/
dmg (at) uvic (dot) ca
replace (at) with @ and (dot) with .


More information about the Discuss mailing list