16.3. Working with Files and Directories

 <  Day Day Up  >  

16.2. Navigating in Unix

If 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 flags

Between 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:

  • -a . The unadorned ls command even displays the names of invisible files and folders ”at least by the Finder's definition. The Unix shell uses its own system of denoting invisible files and folders, and ignores the Finder's. That doesn't mean you're seeing everything; files that are invisible by the Unix definition still don't show up.

    You can use one of the ls command's flags, however, to force even Unix-invisible files to appear. Just add the -a flag. In other words, type this: ls -a . Now when you press Enter, you might see something like this:

     . Desktop Music .. Documents  Pictures .CFUserTextEncoding  Library Public .Trash Movies  Sites 

  • -F . As you see, the names of invisible Unix files all begin with a period (Unix folk call them dot files ). But are these files or folders? To find out, use ls with the -F option (capitalization counts), like this: ls -aF . You're shown something like this:

     ./ Desktop/ Music/ ../ Documents/  Pictures/ .CFUserTextEncoding  Library/ Public/ .Trash/ Movies/  Sites/ 

    The names of the items themselves haven't changed, but the -F flag makes slashes appear on directory (folder) names. This example shows that in your home directory, there are 11 other directories and one file.

  • -G . Here's a fascinating flag that makes ls display color -coded results: blue for directories, red for programs, normal black-on-white type for documents, and so on.

  • -R . The -R flag produces a recursive listing ”one that shows you the directories within the directories in the list. Listing all of the home directory could take several pages, but if you type ls -R Movies , for example, you might get something like this:

     Bad Reviews.doc Old Tahoe Footage 2 Picnic Movie 2  Reviews.doc ./Old Tahoe Footage 2: Tahoe 1.mov   Tahoe 3.mov   Tahoe Project File Tahoe 2.mov   Tahoe 4.mov ./Picnic Movie 2: Icon? Media Picnic Movie 2 Project ./Picnic Movie 2/Media: Picnic Movie 1 Picnic Movie 3 Picnic Movie 5 Picnic Movie 2 Picnic Movie 4 Picnic Movie 6 

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 :

  • cd /Users/chris/Movies . That's the long way ”the absolute pathname. It works no matter what your working directory is.

  • cd ~/Movies . This, too, is an absolute pathname that you could type from anywhere . It relies on the ~ shorthand (which means "my home directory," unless you follow the ~ with another account name ).

    Figure 16-3. This may be the quickest way of all to identify a directory or file you want to manipulate: Don't type anything. When you drag icons directly from the desktop into a Terminal window, the icon's pathname appears automatically at the insertion point. Terminal even adds backslashes to any special characters in these pathnames for you (a necessary step known as escaping the special characters ; see Section 16.2.5.3).


  • cd Movies . This streamlined relative path exploits the fact that you're already in your home directory.


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).

Figure 16-4. The double dot tells Unix to switch its attention to the Movies directory (walking upward through the directory tree); the rest tells it to walk down the Movies directory into the Shorts directory. Note that the prompt always identifies the current working directory.


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 Features

By 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 completion

You 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.

Figure 16-5. Top: You type cd /U and then press Tab. Second from top: Terminal finishes the directory name Users for you. Third from top: You type c and then press Tab.
Bottom: Terminal finishes the homedirectory name, chris. You can also use tab completion to specify file names, as when you type ls -l Movies/R and then press Tab; Terminal finishes the name Reviews.doc.


Some tips for tab completion:

  • Capitalization counts.

  • Terminal adds backslashes automatically if your directory names include spaces, $ signs, or other special characters. But you still have to insert your own backslashes when you type the "hint" characters that tip off tab completion.

  • If it can't find a match for what you typed, Terminal beeps.

    If it finds several files or directories that match what you typed, Terminal beeps; when you press Tab again, terminal shows you a list of them. To specify the one you really wanted, type the next letter or two and then press Tab again.

16.2.5.2. Using the history

You 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. Wildcards

Wildcards 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.

WORKAROUND WORKSHOP
No Spaces Allowed

Terminal doesn't see a space as a space. It thinks that a space means, for example, "I've just typed a command, and what follows is an argument." If you want to see what's in your Short Films directory, therefore, don't bother typing ls ~/Movies/Short Films . You'll only get a "No such file or directory" error message. (See the space in the Short Films directory name?)

Similarly, % signs have a special meaning in Unix. If you try to type one in a pathname (because a directory name contains %, for example), you'll have nothing but trouble.

Fortunately, you can work around this quirk by using a third reserved, or special, character: the backslash (\). It says, "Ignore the special meaning of the next character ”a space, for example. I'm not using it for some special Unix meaning. I'm using the following space as, well, a space." (At a Unix user- group meeting, you might hear someone say, "Use the slash character to escape the space character.")

The correct way to see what's in your Short Films directory, then, would be ls ~/Movies/Short\Films . (Note how the backslash just before the space means, "This is just a space ”keep it moving, folks.")

Of course, if you have to enter a lot of text with spaces, it'd be a real pain to type the backslash before every single one. Fortunately, instead of using backslashes, you can enclose the whole mess with single quotation marks. That is, instead of typing this:

cd/Users/chris/My\ Documents/Letters\ to\ finish/Letter\ to\ Craig.doc

you could just type this:

cd '/Users/chris/My Documents/Letters to finish/Letter to Craig.doc'

It can get even more complicated. For example, what if there's a single quote in the path? (Answer: Protect it with double quotes.) Ah, but you have years of study ahead of you, grasshoppa.


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 switching

A 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 ~ shortcut

You 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 keys

The 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.

Keystroke

Effect

Control-U

Erases the entire command line you're working on (to the insertion point's left).

Control-A

Moves the insertion point to the beginning of the line.

Control-E

Moves the insertion point to the end of the line.

Control-T

Transposes the previous two characters.

Esc-F

Moves the insertion point to the beginning of the next word.

Esc-B

Moves the insertion point to the beginning of the current word.

Esc-Delete

Erases the previous word (defined as "anything that ends with a space, slash, or most other punctuation marks; periods and asterisks not included"). You have to hold down Esc as you press Delete; repeat for each word.

Esc-D

Erases the word, or section of a word, following the insertion point.

Esc-C

Capitalizes the letter following the insertion point.

Esc-U

Changes the next word or word section to all uppercase letters.

Esc-L

Changes the next word or word section to all lowercase letters.


 <  Day Day Up  >  


Mac OS X. The Missing Manual
Mac OS X Snow Leopard: The Missing Manual (Missing Manuals)
ISBN: 0596153287
EAN: 2147483647
Year: 2005
Pages: 506
Authors: David Pogue

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net