Directory Nicknames
Programmers often have to deal with files in dozens of different
directories, and some tasks require a lot of moving around the file system.
This can involve a lot of typing lengthy cd commands!
Here's a cute trick to make it easy.
The idea is to define nicknames for commonly used directories.
These nicknames are implemented as shell variables.
The nickname definitions are stored in ~/.dirs.
Each line is of the form nickname=full_directory_path (e.g. b=/usr/local/bin).
Now we create a script named "c" which acts like "cd" but takes a nickname
as argument.
Here it is:
#!/bin/sh
a=`cat ~/.dirs | grep "^$1=" | sed -e 's/.*=//'`
echo $a
echo "cd $a" > /tmp/.x
source /tmp/.x
It's a bit obscure, but basically it searches the .dirs file for the
nickname, then writes a cd command to a temporary file, and sources
the temporary file.
It's possible on some systems to avoid the temporary file, if the
shell supports aliases with arguments.
Now it's easy to move around the file system, if you've set up
nicknames for commonly visited directories:
> c bin
> ... do something ...
> c s
> ... do something else ...
> c src
> ... do another thing ...
There's more!
If you put a "source ~/.dirs" in your .rc init script,
then all the nicknames become shell variables, which you
can use directly in commands, as in:
> cp $bin/*.x $src/*.x
This vastly simplifies the task of moving files around - just use
the nickname instead of the entire path.
If you want to add a new nickname to the list, here's a little
script, named "save" to do it.
You just cd to the directory, and type 'save nickname':
#!/bin/sh
a=`pwd`
echo "$1=$a" >> ~/.dirs
Finally, it's often useful to see the list of nicknames you've
defined.
Create a script named "show" in your path:
#!/bin/sh
cat ~/.dirs