Journal jomama717's Journal: Bash "cd" wrapper 4
Below is some bash script I wrote years ago that allows you to "anchor" up to 5 directories for fast access later by entering the command "cd [1-5]". You can assign a number to the current directory by typing "cd -s [1-5]", and dump the current assignments with "cd -p". Aside from these new options cd operates as normal (unless you have directories named 1,2,3,4, or 5). There is also a "dir_persist" function that saves off the current set of anchors under a given tag and can be loaded up after a subsequent login. I find that this simple change allows me to navigate a large source repository much faster than before (no more cd ../../../../../../../other_component). Note that this is the "cygwinized" version, which should run on unix but I've never tried it. I originally only used it only on unix machines, and had to jump through some hoops to handle directories with spaces in the names on cygwin.
CD_USAGE="cd: Usage: cd [ -s [ 1-5 | [1-5]=<directory> ] | -p | <directory name> | [1-5] ]";
function dir_persist {
DIRSTRINGS="ONE_DC TWO_DC THREE_DC FOUR_DC FIVE_DC";
_DIRSTRINGS=($DIRSTRINGS);
if [ $# -eq 0 ]; then
echo -n "Enter a name to persist dirs under, or <enter> to skip: "
read session_tag
else
session_tag=${1}
fi
if [ "${session_tag}" = "" ]; then
CDFILE_NAME=".cdfile_${HOSTNAME}_default"
else
CDFILE_NAME=".cdfile_${HOSTNAME}_${session_tag}"
fi
let count=0;
while [ $count -lt 5 ] ; do
outp=`env | grep "${_DIRSTRINGS[$count]}"`;
if [ "" != "$outp" ] ; then
dir_to_print=`echo $outp | cut -d"=" -f2`;
let num=count+1;
if [ $count -eq 0 ] ; then
echo "$num=$dir_to_print" > ~/${CDFILE_NAME};
else
echo "$num=$dir_to_print" >> ~/${CDFILE_NAME};
fi
fi
let count=count+1;
done
}
function dirset {
DIRSTRINGS="ONE_DC TWO_DC THREE_DC FOUR_DC FIVE_DC";
_DIRSTRINGS=($DIRSTRINGS);
let err=0;
if [ "$#" -ge 2 ] || [ "$#" -eq 0 ] ; then
let err=1;
fi
if [ -z `echo "$1" | grep "="` ] ; then
let reg=$1;
let pwd_flag=1;
else
let reg=`echo "$1" | cut -d"=" -f1`;
dir=`echo "$1" | cut -d"=" -f2 | tr '~' ' '`;
let pwd_flag=0;
fi
if [ $reg -gt 5 ] || [ $reg -eq 0 ] ; then
echo >&2 "cd: Invalid directory choice";
let err=1;
fi
if [ $err -eq 0 ] ; then
let reg=reg-1;
if [ $pwd_flag -eq 1 ] ; then
littlePWD="${PWD}"
export "${_DIRSTRINGS[$reg]}=${littlePWD}";
else
littleDir="${dir}"
export "${_DIRSTRINGS[$reg]}=${littleDir}";
fi
fi
}
function cd {
DIRSTRINGS="ONE_DC TWO_DC THREE_DC FOUR_DC FIVE_DC"
_DIRSTRINGS=($DIRSTRINGS)
let err=0
case $# in
0)
command cd
return
;;
1)
if [ "$1" = "-p" ] ; then
let count=0
while [ "$count" -lt 5 ] ; do
outp=`env | grep "${_DIRSTRINGS[$count]}"`
if [ "" != "$outp" ] ; then
dir_to_print=`echo $outp | cut -d"=" -f2`
let num=count+1
echo "$num) $dir_to_print"
fi
let count=count+1
done
elif [ "$1" = 1 -o "$1" = 2 -o "$1" = 3 -o "$1" = 4 -o "$1" = 5 ] ; then
let choice=$1
let choice=choice-1
_CD_STRING="\$${_DIRSTRINGS[$choice]}"
CD_STRING=`eval echo "${_CD_STRING}"`
command cd "${CD_STRING}"
else
command cd "${1}"
return
fi
;;
2)
teststr=`echo $1 | grep "\-s"`
if [ -z "$teststr" ] ; then
echo >&2 "$CD_USAGE"
let err=1
return
fi
dirset "$2"
;;
*)
echo >&2 "$CD_USAGE"
let err=1
return
;;
esac
}
CD_USAGE="cd: Usage: cd [ -s [ 1-5 | [1-5]=<directory> ] | -p | <directory name> | [1-5] ]";
function dir_persist {
DIRSTRINGS="ONE_DC TWO_DC THREE_DC FOUR_DC FIVE_DC";
_DIRSTRINGS=($DIRSTRINGS);
if [ $# -eq 0 ]; then
echo -n "Enter a name to persist dirs under, or <enter> to skip: "
read session_tag
else
session_tag=${1}
fi
if [ "${session_tag}" = "" ]; then
CDFILE_NAME=".cdfile_${HOSTNAME}_default"
else
CDFILE_NAME=".cdfile_${HOSTNAME}_${session_tag}"
fi
let count=0;
while [ $count -lt 5 ] ; do
outp=`env | grep "${_DIRSTRINGS[$count]}"`;
if [ "" != "$outp" ] ; then
dir_to_print=`echo $outp | cut -d"=" -f2`;
let num=count+1;
if [ $count -eq 0 ] ; then
echo "$num=$dir_to_print" > ~/${CDFILE_NAME};
else
echo "$num=$dir_to_print" >> ~/${CDFILE_NAME};
fi
fi
let count=count+1;
done
}
function dirset {
DIRSTRINGS="ONE_DC TWO_DC THREE_DC FOUR_DC FIVE_DC";
_DIRSTRINGS=($DIRSTRINGS);
let err=0;
if [ "$#" -ge 2 ] || [ "$#" -eq 0 ] ; then
let err=1;
fi
if [ -z `echo "$1" | grep "="` ] ; then
let reg=$1;
let pwd_flag=1;
else
let reg=`echo "$1" | cut -d"=" -f1`;
dir=`echo "$1" | cut -d"=" -f2 | tr '~' ' '`;
let pwd_flag=0;
fi
if [ $reg -gt 5 ] || [ $reg -eq 0 ] ; then
echo >&2 "cd: Invalid directory choice";
let err=1;
fi
if [ $err -eq 0 ] ; then
let reg=reg-1;
if [ $pwd_flag -eq 1 ] ; then
littlePWD="${PWD}"
export "${_DIRSTRINGS[$reg]}=${littlePWD}";
else
littleDir="${dir}"
export "${_DIRSTRINGS[$reg]}=${littleDir}";
fi
fi
}
function cd {
DIRSTRINGS="ONE_DC TWO_DC THREE_DC FOUR_DC FIVE_DC"
_DIRSTRINGS=($DIRSTRINGS)
let err=0
case $# in
0)
command cd
return
;;
1)
if [ "$1" = "-p" ] ; then
let count=0
while [ "$count" -lt 5 ] ; do
outp=`env | grep "${_DIRSTRINGS[$count]}"`
if [ "" != "$outp" ] ; then
dir_to_print=`echo $outp | cut -d"=" -f2`
let num=count+1
echo "$num) $dir_to_print"
fi
let count=count+1
done
elif [ "$1" = 1 -o "$1" = 2 -o "$1" = 3 -o "$1" = 4 -o "$1" = 5 ] ; then
let choice=$1
let choice=choice-1
_CD_STRING="\$${_DIRSTRINGS[$choice]}"
CD_STRING=`eval echo "${_CD_STRING}"`
command cd "${CD_STRING}"
else
command cd "${1}"
return
fi
;;
2)
teststr=`echo $1 | grep "\-s"`
if [ -z "$teststr" ] ; then
echo >&2 "$CD_USAGE"
let err=1
return
fi
dirset "$2"
;;
*)
echo >&2 "$CD_USAGE"
let err=1
return
;;
esac
}
:-) thanks (Score:1)
Am trying to get some stuff to work - and I have useful informative names on the directories - which makes them very long
Re: (Score:1)
Re: (Score:1)
ian@tqft:~$ sudo
Password:
ian@tqft:~$ cd
ian@tqft:/media/hde1/home/ian/mozilla$ cd 1
bash: cd: 1: No such file or directory
ian@tqft:/media/hde1/home/ian/mozilla$ cd -s 1
bash: cd: -s: invalid option
cd: usage: cd [-L|-P] [dir]
ian@tqft:/media/hde1/home/ian/mozilla$ cd -s1
bash: cd: -s: invalid option
cd: usage: cd [-L|-P] [dir]
ian@tqft:/media/hde1/home/ian/mozilla$
what did I
Re: (Score:1)