Mac Os X 10.4 Tiger (Visual Quickstart Guides)

Up to now, you haven't moved around in the directory tree. You've been fixed in place in your home directory. Changing directories is easyjust use the cd (change directory) command, followed by the destination you want to move to.

You have two ways to indicate a destination: with an absolute path or a relative path.

  • An absolute path specifies the location of a file or subdirectory, starting at the root directory and working downward.

  • A relative path specifies the location of a file or subdirectory starting at your present location.

Let's look at an example. Suppose I'm currently in my home directory (/Users/ronh) and I want to move to the /usr/bin directory. I could specify the destination with its absolute path: /usr/bin. Or I could use the relative path to go up two directories to the root and then down two directories to the one I want. This is where the special "double-dot" (..) directory name that I discussed earlier comes into play; it indicates the directory above the current one. So the relative path from my home directory to /usr/bin would be ../../usr/bin.

Tips

  • The cd command does not have any options and has no man page of its own because it's built in to the shell. You can find out more about the shell by using the man pages; enter man bash and press .

  • Absolute paths, which always start with a forward slash (/), work no matter where you are located in the Unix file system because they start from the root directory.

  • Relative paths are especially useful if you are deep inside the directory structure and want to access a file or subdirectory just one level up. For example, it's a lot easier to type ../images/flower.jpg than /Users/ronh/Documents/ClipArt/Plants/Color/images/flower.jpgand it's a lot easier to remember, too!

To change directories using an absolute path

Type cd followed by the absolute path to the directory you want (for example, cd /usr/bin) and press .

The current directory changes and the path to the directory appears in the shell prompt (Figure 11).

Figure 11. Here's how you can change the current directory to /usr/bin using an absolute path…

To change directories using a relative path

Type cd followed by the relative path to the directory you want (for example, from your home directory, type cd ../../usr/bin) and press .

The current directory changes and the path to the directory appears in the shell prompt (Figure 12).

Figure 12. …or a relative path from your home directory.

To move to a subdirectory using an absolute path

Type cd followed by the absolute path to the subdirectory (for example, cd /Users/ronh/Sites) and press .

The current directory changes and the path to the directory appears in the shell prompt (Figure 13).

Figure 13. Here's how you can move to a subdirectory using an absolute path…

To move to a subdirectory using a relative path

Type cd followed by the relative path to the subdirectory (for example, from your home directory, type cd Sites) and press .

The current directory changes and the path to the directory appears in the shell prompt (Figure 14).

Figure 14. …or with a relative reference.

Tip

  • Do not include a forward slash (/) before a subdirectory name. Doing so tells Unix to start at the root directory (as if you were entering an absolute path) and could result in an error message (Figure 15).

    Figure 15. If you enter an incorrect path, an error message appears.

To list the contents of the current directory

Type ls and press .

The contents of the directory appear in the Terminal window (Figure 16).

Figure 16. Once you're in a directory, using the ls command by itself displays the contents of that directory.

To return to your home directory

Type cd (without any arguments) and press .

The current directory changes to your home directory and the tilde (~) character appears in the shell prompt.

Tip

  • The tilde character is Unix shorthand for "home directory." So, for example, if you wanted to change to the home directory for user name rosef, you could type cd ~rosef.

Категории