Tải bản đầy đủ (.pdf) (10 trang)

Tài liệu 16.2. Navigating in Unix ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (43.81 KB, 10 trang )

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?
UP TO SPEED
Pathnames 101
In many ways, browsing the contents of your hard drive using Terminal is just
like doing so with the Finder. You start with a folder, and move down into its
subfolders, or up into its parent folders.
In this chapter, you'll be asked to specify a certain file or folder in this tree of
folders. But you can't see their icons from the command line. So how are you
supposed to identify the file or folder you want?
By typing its pathname. The pathname is a string of folder names, something
like a map, that takes you from the root level to the next nested folder, to the
next, and so on. (The root level is, for learning-Unix purposes, the equivalent of
your main hard drive window. It's represented in Unix by a single slash. The
phrase /Users, in other words, means "the Users folder in my hard drive
window"—or, in Unix terms, "the Users directory at the root level.")
One way to refer to the Documents folder in your own Home folder, for
example, would be /Users/chris/Documents (if your name is Chris, that is).

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 Downloads Movies Pictures Sites
Documents Library Music Public

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
further 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. Arguments are extra pieces of
information following the command that refine how the command should run.
(Remember the calendar example? When you wanted the June 2008 calendar, you typed

cal 6 2008. The "6" and "2008" parts were the arguments—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
As part of a command's arguments, you can sometimes insert option flags (also called
switches)—modifying characters (or short phrases) that affect how the command works,
just like option settings do in GUI applications. 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 Downloads Public
.DS_Store Library Sites
.Trash Movies



-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 Downloads/ Public/

.DS_Store Library/ Sites/

.Trash/ Movies/

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 12 other directories and two files.

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

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.


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

×