Книга: Fedora™ Unleashed, 2008 edition

Changing Directories with cd

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 itself — it is built in to bash (or whichever shell interpreter you are using), but it is still used like a command.

The most basic use of cd is this:

$ cd somedir

That command looks in the current directory for the somedir subdirectory, and 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:

[paul@caitlin ~]$ cd /usr/local
[paul@caitlin local]$ cd bin
[paul@caitlin bin]$ cd -
/usr/local
[paul@caitlin local]$ cd ~
[paul@caitlin ~]$

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 the 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 subdirectory — for example, cd subdir — you can tell cd to what location you would like that to be relative. 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.

The 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 [paul@caitlin empty]$ pwd
2 /home/paul/empty
3 [paul@caitlin empty]$ ls
4 [paul@caitlin empty]$ mkdir local
5 [paul@caitlin empty]$ ls
6 local
7 [paul@caitlin empty]$ cd local
8 [paul@caitlin local]$ cd ..
9 [paul@caitlin empty]$ export CDPATH=/usr
10 [paul@caitlin empty]$ cd local
11 /usr/local
12 [paul@caitlin empty]$ cd -
13 /home/paul/empty
14 [paul@caitlin empty]$ export CDPATH=.:/usr
15 [paul@caitlin empty]$ cd local
16 /home/paul/empty/local
17 [paul@caitlin local]$

Lines 1-3 show that you are in /home/paul/empty and that it is indeed empty — ls had no output. Lines 4-6 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 Fedora has the directory /usr/local, which means the current directory (/home/paul/empty) and the CDPATH directory (/usr) both have a local subdirectory. In line 10, while in the /home/paul/empty directory, cd local is used. This time, bash switches to /usr/local and even prints the new directory to ensure that you know what it has done.

Lines 12 and 13 move you 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 directory — it does that whenever it looks up a directory in CDPATH.

Оглавление книги


Генерация: 0.675. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз