Skip to content

Instantly share code, notes, and snippets.

@coolreader18
Last active March 17, 2018 05:37
Show Gist options
  • Save coolreader18/506fdac2e77eb3929b486bb9fd226031 to your computer and use it in GitHub Desktop.
Save coolreader18/506fdac2e77eb3929b486bb9fd226031 to your computer and use it in GitHub Desktop.
Better cdb - original taken from https://stackoverflow.com/questions/7374534/directory-bookmarking-for-bash, modified slightly.
#!/bin/bash
function cdb() {
USAGE="Usage: cdb ([-c|-d] <bookmark>[/dir[/...]]|-l|-h)" ;
if [ ! -e ~/.cd_bookmarks ] ; then
mkdir ~/.cd_bookmarks
fi
case $1 in
# create bookmark
-c) shift
if [ ! -f ~/.cd_bookmarks/$1 ] ; then
echo "`pwd`" > ~/.cd_bookmarks/"$1" ;
else
echo "Try again! Looks like there is already a bookmark '$1'"
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
for bookmark in ~/.cd_bookmarks/*; do
echo "$(basename $bookmark): $(< $bookmark)"
done
;;
-h) shift
echo $USAGE
;;
# goto bookmark
*)
if [ "$1" == "-g" ] ; then
shift
fi
#if [ -f ~/.cd_bookmarks/$1 ] ; then
dirarr=(${1//"/"/ })
cd $(< ~/.cd_bookmarks/"${dirarr[0]}")
for dir in "${dirarr[@]:1}"; do
cd $dir
done
#else
# echo "Mmm...looks like your bookmark has spontaneously combusted. What I mean to say is that your bookmark does not exist." ;
#fi
;;
esac
}

Installation

Download .cdb, put it into $HOME, put

if [ -f ~/.cdb ]; then . ~/.cdb; fi 

into ~/.bashrc, you're good!

Usage

cdb ([-c|-d] <bookmark>|<bookmark>[/dir[/...]]|-l|-h)

c: create
d: delete
just bookmark: go to that bookmark
l: list
h: help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment