LinuxBasics.org

The community that helps people to run Linux

rss
Table of Contents

Directory-Bookmarks

Typing long path names can really be annoying sometimes. Using shell-variables, it you can build a bookmark-list for your bash.

I had the idea for this while reading this article about CDargs. While CDargs comes with a nice directory-browser, these bookmarks don’t work with all commands. There are special commands for changing directories, copying and moving files, but if you want to do something else (like opening files without changing directories), you will not be able to use your bookmark-list.

Using shell-variables as directory-bookmarks is a nice example of the power and flexibility of the unix-shell.

Bash-variables to the rescue

In bash, you can define variables like this:

export EDITOR=emacs
export FN=`date +%Y-%m`
export DOC=/usr/share/doc/packages/

This defines three variables:

These variables can be used in any command-line by using a dollar-sign and their name. Bash (the command-line) replaces $FN with the value currently stored in the FN-variable.

stw@sixty-four:~> echo $EDITOR
emacs
stw@sixty-four:~> echo $FN
2005-09
stw@sixty-four:~> echo $DOC
/usr/share/doc/packages/
stw@sixty-four:~> cd $DOC
stw@sixty-four:/usr/share/doc/packages> 

Building a bookmark-file

To store my bookmarks, I created a file “.dir-bookmarks” which looks like this:

export DOC=/usr/share/doc/packages
export LinuxSRC=/usr/src/linux
export HOTEL=/SRV/files/Krone/Hotel/2005
export RESTAURANT=/SRV/files/Krone/Restaurant

When a new shell-session is started (by logging into the text-console or opening an xterm), I need to read in these definitions with the command:

. ~/.dir-bookmarks

In order to save typing (which is the ultimate goal of this), I add that line to ~/.bashrc (and on some distros to ~/.profile for login-shells). Together with some error-checking and a system-wide bookmark-file in /etc this looks like:

test -f /etc/dir-bookmarks && . /etc/dir-bookmarks
test -f ~/.dir-bookmarks && . ~/.dir-bookmarks

Using the bookmarks

These directory-bookmarks can be used in any and all shell-commands:

stw@sixty-four:~> du -sh $DOC
313M    /usr/share/doc/packages
stw@sixty-four:~> ls $RESTAURANT | wc -l
88
stw@sixty-four:~> soffice $HO       <---------------------------------- Now I pressed the <TAB>-key twice
$HOME      $HOST      $HOSTFILE  $HOSTNAME  $HOSTTYPE  $HOTEL
stw@sixty-four:~> soffice $HOTEL/   <---------------------------------- Now I pressed the <TAB>-key
stw@sixty-four:~> soffice /SRV/files/Krone/Hotel/2005/Pauschalen/ <---- Now I pressed the <TAB>-key twice
2004-07-02 Entdeckerangebot.pdf     2005-11-26 ErsterAdvent.pdf
2004-07-02 Entdeckerangebot.sxw     2005-11-26 ErsterAdvent.sxw
2004-07-02 Entdeckerangebot.xhtml   2005-12-02 ZweiterAdvent.pdf
2004-07-04 Radler.pdf               2005-12-02 ZweiterAdvent.sxw
[...]
stw@sixty-four:~> soffice /SRV/files/Krone/Hotel/2005/Pauschalen/2005-03-25\ Ostern.sxw

Notice the three occasions when I presses the TAB-key. On the first ocasion, bash behaved like I was doing filename-autocompletion, but it listed the available Variables that begin with “$HO”.
On the second occasion, bash expanded the variable and I was able to continue as is I had typed the whole long path.

Adding bookmarks without editing ~/.dir-bookmarks

With this script, we can easily add the current directory to our bookmark-list. Store it as “addbookmark” anywhere in your $PATH (maybe even /usr/bin):

#! /bin/bash
echo "export "$1"="`pwd` >>~/.dir-bookmarks
. ~/.dir-bookmarks

Here is an example:

stw@sixty-four:~> ./addbookmarks HERE
stw@sixty-four:~> cd $DOC/
stw@sixty-four:/usr/share/doc/packages> cd $HERE

Stefan Waidele jun. 2005/09/25 19:34


Copyright (c) by the authors.
This section of the wiki is licensed under the terms of the GNU Free Documentation License.
See the LBook-licensing page for details.


Linux® is a registered trademark of Linus Torvalds.


 
  tutorials/advanced/realworld/directory-bookmarks.txt · Last modified: 2008/07/20 19:08

LinuxBasics.org

Start Linux-Course Tutorials Linux Links Security Blog Forum E-mail List Search Online Chat

Site-Info

Help Get in Touch Making of LBo

Wiki-Control

Powered by

Linux Apache DokuWiki Mailman RUTE ht://Dig