add this alias to you .bashrc and .bash_profile for this to work properly, otherwise it wont change dirs for you:
alias sd=". sd"
Code:
#!/bin/bash
sdfile="$HOME/.stored-paths"
function delete_entry {
newfile=""
firstline=1;
while read line; do
i=`echo $line | cut -d\ -f1`
if [[ "$i" != "$1" ]] ; then
if [[ "$firstline" == "0" ]] ; then
newfile="$newfile"$'\n'
else
firstline=0
fi
newfile="$newfile""$line"
fi
done < $sdfile
echo "$newfile" > $sdfile
# eval echo "\${newfile%\$'\n'}" #> #sdfile
}
function store_entry {
while read line; do
i=`echo $line | cut -d\ -f1`
if [[ "$i" == "$1" ]] ; then
delete_entry "$i"
break
fi
done < $sdfile
echo "$1 $2" >> $sdfile
sort < $sdfile -o $sdfile
}
function change_dir {
while read line; do
i=`echo $line | cut -d\ -f1`
if [[ "$1" == "$i" ]] ; then
dir="`echo $line | cut -f 2 -d\ `"
cd "$dir"
echo "$dir"
return 0
fi
done < $sdfile
echo "directory identifier not found." >&2
return 1
}
if [[ -f $sdfile ]]; then
echo > /dev/null
else
touch $sdfile
fi
if [[ "$1" == "--clear" ]] ; then
printf "" > $sdfile
elif (( $# == 0 )) ; then
cat "$sdfile"
elif [[ "$1" == "-d" ]] ; then
delete_entry "$2"
elif [[ "$2" != "" ]] ; then
store_entry "$1" "$2"
elif [[ "$1" != "" ]] ; then
change_dir "$1"
return "$?"
elif "$1" == "-h" || "$1" == "--help" ; then
echo "sd - store directories for quick cd'ing"
echo "Copyright (C) Brandon Captain. Released under the GPL v2."
echo ""
echo "Usage: sd 1 /usr/src/linux/ # store path at identifier '1'"
echo "Usage: sd bin /usr/bin # store path at identifier 'bin'"
echo "Usage: sd 1 # cd to path at identifier '1'"
echo "Usage: sd # show all paths"
echo "Usage: sd -d 1 # clear path 1"
echo "Usage: sd --clear # clear all paths"
echo "Usage: sd -h # this help screen"
echo "Usage: sd --help # this help screen"
fi