Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory bookmarking for bash

Tags:

Is there any directory bookmarking utility for bash to allow move around faster on the command line?

UPDATE

Thanks guys for the feedback however I created my own simple shell script (feel free to modify/expand it)

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 } 

INSTALL

1./ create a file ~/.cdb and copy the above script into it.

2./ in your ~/.bashrc add the following

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

3./ restart your bash session

USAGE

1./ to create a bookmark

$cd my_project $cdb -c project1 

2./ to goto a bookmark

$cdb -g project1 

3./ to list bookmarks

$cdb -l  

4./ to delete a bookmark

$cdb -d project1 

5./ where are all my bookmarks stored?

$cd ~/.cd_bookmarks 
like image 910
getmizanur Avatar asked Sep 10 '11 20:09

getmizanur


People also ask

How do I bookmark a directory in Linux?

You can simply press Ctrl+D and the current location will be added as a bookmark. This is valid for both Ubuntu 18.04 and 16.04.

How do I list a directory in Bash?

Use the ls Command to List Directories in Bash. We use the ls command to list items in the current directory in Bash. However, we can use */ to print directories only since all directories finish in a / with the -d option to assure that only the directories' names are displayed rather than their contents.

What is $_ in Bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.

What is $() in bash script?

$() Command Substitution According to the official GNU Bash Reference manual: “Command substitution allows the output of a command to replace the command itself.


1 Answers

Also, have a look at CDPATH

A colon-separated list of search paths available to the cd command, similar in function to the $PATH variable for binaries. The $CDPATH variable may be set in the local ~/.bashrc file.

ash$ cd bash-doc bash: cd: bash-doc: No such file or directory  bash$ CDPATH=/usr/share/doc bash$ cd bash-doc /usr/share/doc/bash-doc  bash$ echo $PWD /usr/share/doc/bash-doc 

and

cd - 

It's the command-line equivalent of the back button (takes you to the previous directory you were in).

like image 58
Fredrik Pihl Avatar answered Sep 16 '22 20:09

Fredrik Pihl