Hack 14. Simplify Changing Directories
This hack makes it ridiculously easy to jump right to your favorite directories from the command line.
Every Linux user probably knows there's an environment variable called PATH. When you type a command, your system looks through all the directories defined in the PATH environment variable until it finds the command. If it doesn't find the command by the time it has exhausted all the directories listed in the path, it tells you it can't find the file.
$ cd somewhere
Here's how it works. Assume for a moment that you're an obsessively organized writer who likes to create directory trees that organize files by types, groups, categories, etc. Let's say you organized your documents such that you have the following directories:
Here's what you want to do. You want to be able to jump right to the chapter2 directory without having to type the entire path. Here's how to set the CDPATH environment variable so that you can do that:
$ export CDPATH=:.:/docs/pub/books/oreilly/linuxhacks
Notice that this command does not include any of the chapter directories in the CDPATH. That's because you want the CDPATH to define the directory immediately above the directory you want to find. Remember, the system searches the directories listed in the path for the contents you specify on the command line. The argument chapter2 is what you're looking for. chapter2 exists in the directory /docs/pub/oreilly/linuxhacks, so that is the directory you want to include in your search CDPATH.
Assuming you have defined CDPATH as shown in the previous example, now you should be able to type this command, (almost) no matter what your current directory might be:
$ cd chapter2
The command should take you immediately to the directory /docs/pub/books/oreilly/linuxhacks/chapter2.
2.6.1. The Catch
Remember I said you're obsessive about organization? Suppose you also created the following directories according to the same pattern mentioned earlier:
Naturally, you have to add a new path to your CDPATH environment variable:
$ export CDPATH=:.:/docs/pub/books/oreilly/linuxhacks:/docs/pub/books/oreilly/bsdhacks
As with the PATH environment variable, you separate different search paths with a colon. The system searches through each path from left to right. Now issue this command:
$ cd chapter2
Where do you think it takes you? It puts you in the same place as before, /docs/pub/books/oreilly/linuxhacks/chapter2. That is not what you expected or desired. But it happened because the cd command searches the CDPATH environment variable paths from left to right. It searched the linuxhacks location first and found chapter2. So, that's where it assumed you wanted to go.
2.6.2. The Solution
One solution is to avoid being obsessive about standardizing the way you structure directories. The other, arguably more realistic solution to this problem is to be extremely careful about how you construct your CDPATH environment variable and how you use it. One way to avoid the problem described earlier is to define the CDPATH in this way:
$ export CDPATH=:.:/docs/pub/books/oreilly
You have to do a little extra typing to get where you want to go, but you still save some effort. If you want to get to chapter2 of bsdhacks, you simply type the following:
$ cd bsdhacks/chapter2
2.6.3. One More cd Trick
This is perhaps the simplest of all tricks for cd, yet surprisingly few people know of it. Suppose you are working in the directory /docs/pub/books/oreilly/linuxhacks/chapter2 and then you change to the directory /usr/X11R6/lib/X11/fonts/TrueType. You can jump back to the previous directory simply by typing the following:
$ cd -
This simple command returns you to the last directory you were using before you changed to the current one.