DirB, Directory Bookmarks for Bash
DirB keeps all directory bookmarks in ~/.DirB, a “hidden” subdirectory of the user's home directory. When the file ~/.bashDirB is sourced from within ~/.bashrc, it checks to see whether the ~/.DirB directory exists. If the directory does not exist, it is created. This guarantees that the bookmark repository exists.
Each bookmark has an associated file in the ~/.DirB directory with the same name as the bookmark. The bookmark file contains a one-line command, such as:
$CD /home/Desktop
The shell variable $CD is set by the g and p commands to cd or pushd, respectively, and the variable is expanded by the shell when the bookmark is invoked. In essence, the command g d is transformed into cd /home/Desktop, and p d is transformed into pushd /home/Desktop.
The DirB commands are implemented as Bash functions that do some error checking, determine which action is to be performed, and then invoke a standard command. For example, the g command does a couple checks before invoking the cd command:
# "g" - Go to bookmark
function g () {
# if no arguments, go to the home directory
if [ -z "$1" ]
then
cd
else
# if $1 is in ~/.DirB and does not
# begin with ".", then go to it
if [ -f ~/.DirB/"$1"
-a ${1:0:1} != "." ]
then
# update the bookmark's timestamp a
# and then execute it
touch ~/.DirB/"$1" ;
CD=cd source ~/.DirB/"$1" ;
else
# else just "cd" to the argument,
# usually a directory path of "-"
cd "$1"
fi
fi
}
The function g checks to see whether there is an argument. If $1 is a zero-length string, the user is sent home with a cd invoked with no argument. Otherwise, a check is made to see if the argument is the name of a saved bookmark and the first character of the argument is not a period.
If both conditions are met, the bookmark is run as part of the current shell by sourcing the bookmark file. Before execution, the shell variable $CD is set to the cd command. source is used instead of calling the bookmark as a shell script so that the directory change will affect the current shell. A called script would have a unique shell session that would terminate after the cd or pushd. Thus, it would have no lasting effect on the current shell session.
If the argument is not the name of a bookmark, or if it begins with a period, the cd command is invoked with the argument to go to the specified directory path.
Note that the source command in the g function above starts with a variable assignment:
CD=cd source ~/.DirB/"$1" ;
Bash syntax allows a command to be preceded by one or more variable assignments.
Most DirB commands eventually call cd, pushd or popd to perform the requested action. If one of these standard commands encounters a problem, it issues an error message to the standard error (stderr) stream and exits with a failing return code.
Note that because bookmarks are the names of their associated files in the ~/.DirB repository, they cannot have slashes in their names. If a bookmark cannot be created (most likely due an invalid character in the name), s will print an error message to the standard error:
% s a/d bash: DirB: /home/.DirB/a/b could not be created
An error message will result if an argument to either g or p is neither a bookmark nor a valid directory path:
% p missing bash: pushd: missing: No such file or directory
This will occur if the bookmark name is misspelled or if the bookmark has been removed. A similar error message results from the d and r commands if their arguments are not valid names of a saved bookmark:
% d missing bash: DirB: /home/.DirB/missing does not exist % r missing bash: DirB: /home/.DirB/missing does not exist
If an error is encountered, DirB commands will exit with a failing return code. This behavior allows other Bash scripts to use these functions and take appropriate action in the event of an error.
DirB was created as a set of Bash functions to extend the concepts of bookmarks to Linux directories. It accelerates the movement between frequently accessed directories from the command line or from shell scripts. Although it's a simple tool, I rely upon DirB daily and hope that others will find it useful too.
Ira Chayut is a longtime UNIX/Linux developer, having first worked on version 6 UNIX in 1976. He is the author of C and UNIX reference booklets, runs www.verilog.net, and has given talks on integrated circuit verification. Currently, he is the founder of a consumer products company and is responsible for all of the embedded and DSP programming. He can be reached at ira@dirb.info.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Speed Up Your Web Site with Varnish | Jun 19, 2013 |
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
- Speed Up Your Web Site with Varnish
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Android's Limits
- Web & UI Developer (JavaScript & j Query)
- Reply to comment | Linux Journal
17 min 13 sec ago - Yeah, user namespaces are
1 hour 33 min ago - Cari Uang
5 hours 4 min ago - user namespaces
7 hours 58 min ago - yea
8 hours 24 min ago - One advantage with VMs
10 hours 52 min ago - about info
11 hours 25 min ago - info
11 hours 26 min ago - info
11 hours 27 min ago - info
11 hours 29 min ago
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
you can just add in your
you can just add in your .bash_profile
export hello=~/tmp/hello
source .bash_profile
cd $hello
you can still use the tab to autocomplete
done
you can just add in your
Thanks...
I knew I could export directories like that but it never dawned on me that autocomplete would still work.... duh. One track mind....
When I tested this, I could not get autocomplete to work with cd. It would work with ls or any other program except cd. Dont' know why, probably some bashrc or profile setting I used is blocking it.
----
BTW I just noticed something about using cd in Ubuntu Maverick that I "think" is a new (or just something I never noticed ) feature:
If you have a sub-directory called ~/test/test and when using cd you mistakenly type:
cd ~/text/text
It will take you to ~/test/test -- instead of giving you an error message -- if that is the only directory with similar spelling on you computer.
Personally I think that is wonderful...
Keith
"I have always wished that my computer would be as easy to use as my telephone.
My wish has come true. I no longer know how to use my telephone."
-- Bjarne Stroustrup
Yes...! Something I never could figure out how to do...
Something I always wanted and never could figure out how to do...
Good Idea!
Great Post!
Excellent Explanation!
Thanks.
"I have always wished that my computer would be as easy to use as my telephone.
My wish has come true. I no longer know how to use my telephone."
-- Bjarne Stroustrup