Skip to content

Instantly share code, notes, and snippets.

@artem78
Last active July 15, 2020 17:49
Show Gist options
  • Save artem78/74f63a4439b35c8ca04a54d3af378004 to your computer and use it in GitHub Desktop.
Save artem78/74f63a4439b35c8ca04a54d3af378004 to your computer and use it in GitHub Desktop.
Directory bookmarking for Bash
# Source: https://stackoverflow.com/q/7374534/4108542
function cdb() {
USAGE="Usage: cdb [-c|-g|-d|-l] [bookmark]" ;
if [ ! -e ~/.cd_bookmarks ] ; then
mkdir ~/.cd_bookmarks
fi
case $1 in
# create bookmark
-c) shift
if [ ! -f ~/.cd_bookmarks/$1 ] ; then
echo "cd `pwd`" > ~/.cd_bookmarks/"$1" ;
else
echo "Try again! Looks like there is already a bookmark '$1'"
fi
;;
# goto bookmark
-g) shift
if [ -f ~/.cd_bookmarks/$1 ] ; then
source ~/.cd_bookmarks/"$1"
else
echo "Mmm...looks like your bookmark has spontaneously combusted. What I mean to say is that your bookmark does not exist." ;
fi
;;
# delete bookmark
-d) shift
if [ -f ~/.cd_bookmarks/$1 ] ; then
rm ~/.cd_bookmarks/"$1" ;
else
echo "Oops, forgot to specify the bookmark" ;
fi
;;
# list bookmarks
-l) shift
ls -l ~/.cd_bookmarks/ ;
;;
*) echo "$USAGE" ;
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment