Changing Directories with cd

Changing directories is surely something that has no options, right? Not so. cd is actually more flexible than most people realize. Unlike most of the other commands here, cd is not a command in itselfit is built in to bash (or whichever shell interpreter you are using), but it is still used like a command.

The most basic usage of cd is this:

cd somedir

That looks in the current directory for the somedir subdirectory, then moves you into it. You can also specify an exact location for a directory, like this:

cd/home/paul/stuff/somedir

The first part of cd's magic lies in the characters (- and a dash and a tilde). The first means "switch to my previous directory," and the second means "switch to my home directory." This conversation with cd shows this in action:

[[email protected] ~]$ cd /usr/local [[email protected] local]$ cd bin [[email protected] bin]$ cd -/usr/local

In the first line, we change to /usr/local and get no output from the command. In the second line, we change to bin, which is a subdirectory of /usr/local. Next, cd - is used to change back to the previous directory. This time Bash prints the name of the previous directory so we know where we are. Finally, cd ~ is used to change back to our home directory, although if you want to save an extra few keystrokes, just typing cd by itself is equivalent to cd

The second part of cd's magic is its capability to look for directories in predefined locations. When you specify an absolute path to a directory (that is, one starting with a /), cd always switches to that exact location. However, if you specify a relative subdirectoryfor example, cd subdiryou can tell cd where you would like that to be relative to. This is accomplished with the cdpath environment variable. If this variable is not set, cd always uses the current directory as the base; however, you can set it to any number of other directories.

This next example shows a test of this. It starts in /home/paul/empty, an empty directory, and the lines are numbered for later reference:

1 [[email protected] empty]$ pwd

2 /home/paul/empty

3 [[email protected] empty]$ ls

4 [[email protected] empty]$ mkdir local

5 [[email protected] empty]$ ls

6 local

7 [[email protected] empty]$ cd local

9 [[email protected] empty]$ export CDPATH=/usr

10 [[email protected] empty]$ cd local

11 /usr/local

13 /home/paul/empty

14 [[email protected] empty]$ export CDPATH=.:/usr

15 [[email protected] empty]$ cd local

16 /home/paul/empty/local

17 [[email protected] local]$

Lines 13 show that we are in /home/paul/empty and that it is indeed emptyls had no output. Lines 46 show the local subdirectory being made, so that /home/paul/empty/local exists. Lines 7 and 8 show you can cd into /home/paul/empty/local and back out again.

In line 9, cdpath is set to /usr. This was chosen because Ubuntu has the directory /usr/local, which means our current directory (/home/paul/empty) and our cdpath directory (/usr) both have a local subdirectory. In line 10, while in the /home/paul/empty directory, we use cd local. This time, Bash switches us to /usr/local and even prints the new directory to ensure we know what it has done.

Lines 12 and 13 move us back to the previous directory, /home/paul/empty. In line 14, cdpath is set to be .:/usr. The : is the directory separator, so this means Bash should look first in the current directory, ., and then in the /usr directory. In line 15 cd local is issued again, this time moving to /home/paul/empty/local. Note that Bash has still printed the new directoryit does that whenever it looks up a directory in cdpath.

Was this article helpful?

0 0

Post a comment