When you open a Finder window and work with its icons, that window represents a particular folder. Likewise, when you open a Terminal window, its shell is working “in” some directory. More technically, your shell has a current working directory (analogous to your open Finder window). When you run commands in that shell, they operate relative to the current working directory. Figure 1-7 illustrates this concept. If your shell is “in” the directory /Users/smith/stuff, and you run a command that refers to a file receipt.pdf, then the file is really /Users/smith/stuff/receipt.pdf.

A Finder window (left) and Terminal window (right) displaying the same folder, /Users/smith/stuff
Figure 1-7. A Finder window (left) and Terminal window (right) displaying the same folder, /Users/smith/stuff

If a path begins with a slash, such as /one/two/three, it’s called an absolute path. If not, it’s a relative path, because it’s relative to a shell’s current location in the filesystem. For instance, a relative path a/b/c, when referenced from the current directory /one/two/three, implies the absolute path /one/two/three/a/b/c. In general, if you refer to a relative file path in a shell, the path is relative to your current working directory.

Two special relative paths are . (a single period) and .. (two periods in a row). A single period refers to your current directory, and two periods means your parent directory, one level above. So if your current directory is /one/two/three, then . refers to this directory and .. refers to /one/two. This explains what we did in Reading This Book, when we wrote ./-foo to indicate that -foo was a file in the current directory, and not an option.

You “move” your shell from one directory to another using the cd (“change directory”) command:

cd /one/two/three

More technically, the cd command changes your shell’s current working directory, in this case to /one/two/three. This is an absolute change (since the directory begins with “/”); of course you can make relative moves as well:

cd d              Enter subdirectory dcd ../mydir       Go up to my parent, then into directory mydir

While you’re cd-ing around the filesystem, you must remember which directory you’re “in.” If you need a reminder, run the pwd command to print the name of your current working directory:

pwd
/Users/smith/stuff

File and directory names may contain most characters you expect: letters,[2] digits, periods, dashes, underscores, and most symbols (but not “/”, which is reserved for separating directories). For practical use, however, don’t create names with spaces, asterisks, question marks, parentheses, and other characters that have special meaning to the shell. Otherwise, you’ll need to quote or escape these characters all the time. (See Quoting.)



[2] OS X filenames are case-insensitive, so capital (uppercase) and small (lowercase) letters are equivalent. (This can be changed if you are technically inclined.) Commands, however, are case-sensitive.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.141.202