Project 1. Lose the Mouse. . . but not just yet. Use it to launch Apple's Terminal application, which you'll find in the Utilities folder within Applications at the root of your system disk (Macintosh HD:Applications:Utilities:Terminal.app). You'll see a white screen with black text (Figure 1.1) welcoming you to Darwin (see the sidebar). You may want to Control-click the Terminal icon in the Dock and select Keep in Dock: You'll be using it a lot! Figure 1.1. The Mac OS X Terminal is the gateway to Unix projects.
Note
Now you can lose the mouse. Why? Unix has been around since the late '60s, long before the era of the graphical user interface (GUI), with its windows, icons, and mice. Everything was text based. You didn't point and click; you typed commands. Although modern Unix environments include a GUI, the real power still lies at the Terminal and in typed commands. Clicking a button may be an easy way to perform a prepackaged action, but it doesn't give you the expressive power of a written set of commands. After all, you don't write scripts by clicking a mouse (even AppleScript scripts); you type them. On the line following the welcome message, you'll see a prompt followed by a cursor. The prompt, as its name suggests, is prompting you, saying, "Terminal is ready and waiting for you to type something." Your First CommandsYou are now expected to type a command on the command line and press Return. Unix will then execute the command, and the results, if any, are written back to the Terminal below the command. When a command is not expected to yield any visible results, it will be silent. Accept the philosophy that "no news is good news." Type the following on the command line: $ ls Desktop Library Music Public Documents Movies Pictures Sites $ So what have we just done? Typing ls at the prompt launches a Unix command (or application) called ls. The lines that follow represent the results, or output, of running that command. You should see a list of the files in your home directory. As you might have guessed, ls is short for list. The output from ls should correspond to what you see in the Mac OS X Finder. It's the same information, presented textually rather than graphically. Next, display the current working directory (CWD). The CWD is an important concept and is discussed, together with absolute and relative pathnames, in Project 2. Issue the following command: $ pwd /Users/your-username-here $ The command pwd will print (display on the terminal screen) the CWD and should display the absolute pathname to your home directory. In Finder terms, the pwd output says, "the folder your-username-here, inside the folder Users at the top level of the system disk." Note
Find Out What the Finder Doesn't FindLet's try some more commands. Compare the output you get (not shown here) with what you see in the Finder. First, list the files in the directory /Usersthe directory one above your home directory. This example gives one argument to ls, which is the directory to list. A command and its argument(s) must be separated by a space. $ ls /Users You may notice that ls lists more files than the Finder shows. The Finder is being slightly dishonest, but in a nice way. It doesn't display Unix system files in which the average user isn't interested. Next, list files at the top level of the system disk (the Macintosh HD). In Unix, the top level of the system disk is termed the root of the file system and is represented by "/". Refer to "Pathnames" in Project 2 for more information. $ ls / Again, you'll notice that ls lists more files than the Finder shows. List both of the above in one command by giving two arguments to ls, separated by a space. $ ls / /Users Next, list the files in your home directory. The two commands below are equivalent because ls without a pathname argument will assume the CWD. $ ls /Users/your-username-here $ ls Finally, list files in the directory Documents within your home directory. Again, the two commands below are equivalent. The CWD, relative pathnames, and absolute pathnames are all significant here. See Project 2 for a full explanation. $ ls /Users/your-username-here/Documents $ ls Documents Command OptionsMost Unix commands can be modified with one or more options, which change or enhance the command's basic functionality. Options always begin with a dash (-) symbol and are one character long. Here are some examples of the options available to ls. Option -l (letter "l") gives a long (more detailed) list. $ ls -l total 0 drwx------ 3 saruman saruman 102 5 May 09:54 Desktop drwx------ 15 saruman saruman 510 4 May 12:11 Documents drwx------ 42 saruman saruman 1428 18 Mar 10:15 Library ... Option -t lists files in time order, with the most recently created or modified files shown first. This example shows that many options can be specified together, separated by a space. $ ls -l -t total 0 drwx------ 3 saruman saruman 102 5 May 09:54 Desktop drwxr-xr-x 5 saruman saruman 170 4 May 22:04 Public drwx------ 15 saruman saruman 510 4 May 12:11 Documents ... Options can be merged, so the following are equivalent. $ ls -l -t $ ls -lt Tip
Note
Cases and SpacesThe Unix file system (UFS) is case sensitive, which means that the file documents is not the same file as Documents: The two files can coexist in the same directory. Likewise, Unix commands treat filenames in a case-sensitive manner. For Mac OS X (or, more correctly, Darwin), the situation is complicated somewhat. The default file system is not UFS, but Apple's HFS+, which is not case sensitive. However, many of the traditional Unix commands remain case sensitive. You can ignore case most of the time, but be warned that occasionally you will get caught out. Spaces in filenames may catch you out, too. Trying to list a directory named My Directory will throw an error. $ ls My Directory ls: Directory: No such file or directory ls: My: No such file or directory $ Recall that multiple arguments are separated by spaces. To indicate that the space in "My Directory" is part of the filename, and not an argument separator, either enclose the file name in quotes, or escape the space with a backslash (\) symbol. $ ls "My Directory" $ ls 'My Directory' $ ls My\ Directory Learn More
All About ShellsIt's possible to use Unix without being aware of what a shell is, but you won't get the most from your command-line experience if you do not understand this concept. Apple's Terminal application is not a shell; it's an Aqua application that provides a window on your Desktop, a means of reading the keyboard, and a means of displaying text. Terminal then launches a Unix program called a shell. The shell displays the prompt. Everything you type at the command line goes to the shell, which interprets what you have typed and acts accordingly. Type ls /Users, for example, and the shell will run the command ls, passing it the argument /Users. The shell will tell ls where to send its output (usually back to the Terminal). After ls has completed, it stops and returns control to the shell, which then issues another prompt, and the sequence repeats. The shell performs many other tasks, including setting up redirection and pipelining (Project 6), running shell scripts (Projects 9 and 10), and implementing command-line editing (discussed later in this chapter). Many shells are available, and Darwin comes with most of them. Have a BashYou are most likely running the Bash shell if your user account was created by Mac OS X 10.3 or later, or if the prompt ends with a dollar symbol. If your user account was created by an earlier version, or if the prompt ends with a percent (%) symbol, you are most likely running the Tcsh shell. To be absolutely certain which, type the following command and examine the shell name (the part following /bin/ on the output line). $ echo $SHELL /bin/bash $ Alternatively, use the application NetInfo Manager (see Figure 1.2), which you will find in the Utilities folder in Applications at the root of your system disk (Macintosh HD:Applications:Utilities:NetInfo Manager.app). Figure 1.2. Apple's NetInfo Manager application shows your default shell.How to Change the Default ShellThe projects in this book are written assuming the Bash shell. Use NetInfo Manager to change your default shell to Bash permanently. Referring to Figure 1.2, click your username. In the lower pane, scroll down until you see shell in the Property pane. Change the value in the Value(s) pane to /bin/bash. You must authenticate first by clicking the lock icon in the lower left corner and entering your administrator password. Press Command-s to save; then click Update this Copy. Close NetInfo and start a new Terminal session. Your prompt should now end with a dollar symbol. If the shell has not changed, quit and restart Terminal. Tip
Learn More
Learn More
Command-Line EditingYou'll probably spend a lot of time using Terminal and Bash, so it's worthwhile getting to know more about them. Bash can be customized in many ways to suit your style of working. The ability to recall and edit previous command lines can be a big time-saverso much so that it's worth mentioning separately here. Press the up arrow key (cursor-up) to recall previous command lines. The left and right arrow keys, and the Backspace key, work as expected to edit the command line. Useful editing keystrokes include:
|