A directory quick-change script in bash

It is convenient to have mnenomic names for favourite directories so that you can change to them quickly. ‘cdargs’ is a directory bookmarking utility that I use quite often, but I find that it can become cluttered, and difficult to determine which bookmark to select. There is a github project called ‘bashmarks’, which I tried a few times, but new stuck with. Perhaps you will have better luck with. I present my own solution below.

The problem with ‘bash’/UNIX is that it seems to throw every obstacle in your way to obtaining a solution. You can’t just write a script, because that will be executed in a separate process, so the new directory wont stick. You can’t just write an alias either, because aliases don’t accept command arguments. Very frustrating. Below, I show how it can be done.

You need 3 things: a script, an auxilliary function, and an alias.

Firstly, you need to write the script that actually does the work. Its job is to print a directory name given an argument. I call mine ‘chdir’, and it should be placed somewhere where $PATH can find it. Here it is:

#!/usr/bin/env bash
# to be used by alias "c" to change directory

>&2 echo "chdir called $1. Use h for help" 

>&2 echo "Quick-change directory.
b bookish
h help

case $1 in
 b) echo "/projects/repos/bookish" ;;
 h) print_help ; echo . ;;
 "repos") echo "/projects/repos" ;;
 *) echo . ;;

You will, of course, have to completely adapt it to suit your own preferences. So, if I want to change to my ‘bookish’ directory, I pass in the argument ‘b’. In the ‘case’ statement, the default is to echo . to stdout. I have also illustrated a case “repos” to demonstrate that the arguments you pass in do not have to be single letters. Finally, if you pass in ‘h’, it will print a helpful message to stderr. This is achieved by prefixing the echo command with ‘>&2’. Note that we don’t print the message to stdout, because stdout is used to print the directory we want to change to. ‘echo .’ will tell cd to change to the current directory.

Next, add the lines in .bashrc:

cdfunc() { cd `chdir $1` ; }
alias c=cdfunc

The first line is an auxilliary function, and the second line is an alias. I have chose to call my command ‘c’. Nice and simple. You might choose another name, but make sure it does not clash with a current or potentially useful program. Maybe ‘bm’, for ‘bookmark’, is a good idea.

The alias ‘c’ is an alias for the function ‘cdfunc’, which takes a argument and passes it to ‘chdir’. ‘chdir takes this argument, and prints the results of a lookup. cdfunc then does a command line substitution to change to that directory. So if I type

c b

I will change to directory /projects/repos/bookish .

Job done.


About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Computers and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s