Skip to content

Instantly share code, notes, and snippets.

@markhibberd
Created April 27, 2011 00:36
Show Gist options
  • Save markhibberd/943494 to your computer and use it in GitHub Desktop.
Save markhibberd/943494 to your computer and use it in GitHub Desktop.
findbase() {
search="$1"
current="$2"
base=`basename $current`
case "$base" in
"."|""|"/")
return 1
;;
"$search")
echo $current
return 0
;;
*)
findbase "$search" "`dirname $current`"
;;
esac
}
cdup() {
if [ $# -ne 1 ]; then
echo "cd up to an ancestor folder of the specified name"
echo "usage: `basename $0` dir"
return 1
fi
target="$1"
directory=`pwd`
result=`findbase $target $directory`
if [ $? -ne 0 ]; then
echo "Could not find ancestor folder [$target]."
return 1
fi
echo $result
cd $result
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment