< Day Day Up > |
16.2. Navigating in UnixIf you can't see any icons for your files and folders, how are you supposed to work with them? You have no choice but to ask Unix to tell you what folder you're looking at (using the pwd command), what's in it (using the ls command), and what folder you want to switch to (using the cd command), as described in the following pages. 16.2.1. pwd (print working directory, or "Where am I?")Here's one of the most basic navigation commands: pwd , which stands for print working directory . The pwd command doesn't actually print anything on your printer. Instead, the pwd command types out, on the screen, the path Unix thinks you're in (the working directory). Try typing pwd and pressing Enter. On the next line, Terminal may show you something like this: /Users/chris/Movies Terminal is revealing the working directory's path ”a list of folders-in-folders, separated by slashes , that specifies a folder's location on your hard drive. /Users/ chris/Movies pinpoints the Movies folder in Chris's Home folder (which, like all Home folders, is in the Users directory). Tip: Capitalization counts in Unix. Command names are almost always all lowercase (like cal and pwd ). But when you type the names of folders , be sure to capitalize correctly. 16.2.2. ls (list, or "What's in here?")The ls command, short for list , makes Terminal type out the names of all the files and folders in the folder you're in (that is, your working directory). You can try it right now: just type ls and then press Enter. Terminal responds by showing you the names of the files and folders inside in a list, like this: Desktop Library Music Public Documents Movies Pictures Sites In other words, you see a list of the icons that, in the Finder, you'd see in your Home folder. Note: Terminal respects the limits of the various Mac OS X accounts (Chapter 12). In other words, a Standard or Administrator account holder isn't generally allowed to peek into someone else's Home folder. If you try, you'll be told, "Permission denied ." You can also make Terminal list what's in any other directory (one that's not the working directory) just by adding its pathname as an argument . An argument tells the command what to work on. (Remember the Calendar example? When you wanted the June 2005 calendar, you typed cal 6 2005 . The "6 2005" part was the argument ”that is, everything you typed after the command itself.) To see a list of the files in your Documents directory, then, you could just type ls/Users/chris/Documents . Better yet, because the ~ symbol is short for "my home directory," you could save time by typing ls~/Documents . The pathname "~/Documents" is an argument that you've fed the ls command. 16.2.2.1. About flagsBetween a command and its arguments, you can sometimes insert option flags (also called switches ) ”short modifying phrases that affect how the command works. In the Calendar example, you can type cal -y to see a full-year calendar; the -y part is an option flag. Option flags are almost always preceded by a hyphen (-), although you can usually run several flags together following just one hyphen. If you type ls -al , both the -a and -l flags are in effect. Here are some useful options for the ls command:
In other words, you've got two subdirectories here, called Old Tahoe Footage 2 and Picnic Movie 2 ”which itself contains a Media directory. Tip: As you can tell by the cal and ls examples, Unix commands are very short. They're often just two-letter commands, and an impressive number of those use alternate hands (ls, cp, rm , and so on).The reason has partly to do with conserving the limited memory of early computers and partly to do with efficiency: most programmers would just as soon type as little as possible to get things done. User -friendly it ain't, but as you type these commands repeatedly over the months, you'll eventually be grateful for the keystroke savings. 16.2.3. cd (change directory, or "Let me see another folder")Now you know how to find out what directory you're in, and how to see what's in it, all without double-clicking any icons. That's great information, but it's just information. How do you do something in your command-line Finder ”like switching to a different directory? To change your working directory, use the cd command, followed by the path of the directory you want to switch to. Want to see what's in the Movies directory of your home directory? Type cd /Users/chris/Movies and press Enter. The $ prompt shows you what it considers to be the directory you're in now (the new working directory). If you perform an ls command at this point, Terminal shows you the contents of your Movies directory. That's a lot of typing, of course. Fortunately, instead of typing out that whole path (the absolute path, as it's called), you can simply specify which directory you want to see relative to the directory you're already in. For example, if your Home folder is the working directory, the relative pathname of the Trailers directory inside the Movies directory would be Movies/Trailers . That's a lot shorter than typing out the full, absolute pathname ( /Users/chris/Movies/Trailers ). If your brain isn't already leaking from the stress, here's a summary of the three different ways you could switch from ~/(your home directory ) to ~/Movies :
Tip: Actually, there's a fourth way to specify a directory that involves no typing at all: dragging the icon of the directory you want to specify directly into the Terminal window. Figure 16-3 should make this clear. 16.2.4. .. (dot-dot, or "Back me out")So now you've burrowed into your Movies directory. How do you back out? Sure, you could type out the full pathname of the directory that encloses Movies ”if you had all afternoon. But there's a shortcut: You can type a a double period (..) in any pathname. This shortcut represents the current directory's parent directory (the directory that contains it). To go from your home directory up to /Users , for example, you could just type cd .. (that is, cd followed by a space and two periods).
You can also use the dot-dot shortcut repeatedly to climb multiple directories at once, like this: cd ../.., which would mean "switch the working directory to the directory two layers out." If you were in your Movies directory, ../.. would change the working directory to the Users directory. Another trick: You can mix the .. shortcut with actual directory names. For example, suppose your Movies directory contains two directories: Trailers and Shorts. Trailers is the current directory, but you want to switch to the Shorts directory. All you'd have to do is type cd ../Shorts , as illustrated in Figure 16-4. 16.2.5. Keystroke-Saving FeaturesBy now, you might be thinking that clicking icons would still be faster than doing all this typing. Here's where the typing shortcuts of the bash shell come in. 16.2.5.1. Tab completionYou know how you can highlight a file in a Finder window by typing the first few characters of its name? The tab-completion feature works much the same way. Over time, it can save you miles of finger movement. It kicks in whenever you're about to type a pathname. Start by typing the first letter or two of the path you want, and then press Tab. Terminal instantly fleshes out the rest of the directory's name. As shown in Figure 16-5, you can repeat this process to specify the next directory-name chunk of the path.
Some tips for tab completion:
16.2.5.2. Using the historyYou may find yourself at some point needing to run a previously entered command, but dreading the prospect of re-entering the whole command. Retyping a command, however, is never necessary. Terminal (or, rather, the shell it's running) remembers the last 500 commands you entered. At any prompt, instead of typing, just press the up or down arrow keys to walk through the various commands in the shell's memory. They flicker by, one at a time, at the $ prompt ”right there on the same line. 16.2.5.3. WildcardsWildcards are special characters that represent other characters ”and they're huge timesavers. The most popular wildcard is the asterisk (*), which means "any text can go here." For example, to see a list of the files in the working directory that end with the letters te , you could type ls *te . Terminal would show you files named Yosemite, BudLite, Brigitte, and so on ”and hide all other files in the list. If the wildcard matches any directories, you'll also see the contents of those directories as well, just as though you'd used ls with each of the full directory names.
Likewise, to see which files and directories begin with "Old," you could type ls Old* and press Enter. You'd be shown only the names of icons in the working directory called Old Yeller, Old Tahoe Footage, Olduvai Software, and so on. If you add the asterisk before and after the search phrase, you'd find items with that phrase anywhere in their names. Typing ls *jo* will show you the files named Mojo, johnson, Major Disaster, and so on. Tip: Using * by itselfmeans "show me everything." To see a list of what's in a directory and in the directories inside it (as though you'd highlighted all the folders in a Finder list view and then pressed c-right arrow), just type ls * . 16.2.5.4. Directory switchingA hyphen (-) after the cd command means, "Take me back to the previous working directory." For example, if you changed your working directory from ~/Movies/Movie 1 to ~/Documents/Letters , simply enter cd - to change back to ~/Movies/Movie 1 . Use cd - a second time to return to ~/Documents/Letters . (Note the space between cd and the hyphen.) Tip: If you're doing a lot of switching between directories, you'll probably find it quicker to open and switch between two Terminal windows , each with a different working directory. 16.2.5.5. The ~ shortcutYou already know that the tilde (~) character is a shortcut to your home directory. But you can also use it as a shortcut to somebody else's home directory simply by tacking on that person's account name. For example, to change to Miho's home directory, use cd ~Miho . 16.2.5.6. Special keysThe bash shell offers dozens of special keystroke shortcuts for navigation. You may recognize many of them as useful undocumented shortcuts that work in any Cocoa application, but even more are available (and useful) in Terminal: Table 16-1.
|
< Day Day Up > |