Project1.Lose the Mouse


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.


What Is Darwin?

Apple's Aqua technology provides all the shiny bits of Mac OS Xthose beautiful icons, windows, and menus. However, underneath in the engine room, Darwin, Mac OS X's Unix foundation, is hard at work. There are many variants of Unix, and Apple chose to base Darwin on FreeBSD (Berkeley Standard Distribution, developed at the University of California, Berkeley). FreeBSD, like a great many other Unix distributions, is open-source software; its source code is freely available, and programmers around the world continually improve and update it. In addition to FreeBSD, Darwin takes advantage of many other open-source technologies, including the Mach kernel, Apache, and GCC.


Note

The prompt you see may differ from that shown in the screen shot in Figure 1.1, depending on which version of OS X you are running and what the default shell is. Shells are explained a little later in this chapter.


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 Commands

You 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

The following conventions are used throughout this book: Do not type the dollar symbol or the space; they represent the prompt. Type exactly what follows. In this example, type ls (letters "ell" and "ess"). Always press Return when you have completed typing; otherwise you and Terminal will sit waiting for each other for an awfully long time. The lines that follow are the output from the command ls. The final line with just a dollar symbol signifies completion of the command and a new prompt. The text you should type is shown in bold.


Directories and Files

In Unix, the term directory is used instead of folder. Folder comes from the Desktop analogy and the visual representation of directories by the GUI. The term file is often used in a general sense to mean both files and directories. Technically, in Unix, a directory is just a file.


Find Out What the Finder Doesn't Find

Let'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 Options

Most 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

Read the Unix manual to find out more about a command and what options it supports. Project 3 shows you how to get the most from the Unix manual.


Note

Many code extracts shown in this book are curtailed (to keep the book to a reasonable size). You'll recognize a curtailed extract because it finishes in an ellipsis (...) rather than the dollar prompt.


Multi-letter Options

Commands that originate from the GNU (GNU is Not Unix) Project from the Free Software Foundation have multi-letter options. So as not to confuse these with merged single-letter options, multi-letter options begin with a double-dash (--) and cannot themselves be merged.


Cases and Spaces

The 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

Project 5 tells you more about the available shells.


All About Shells

It'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 Bash

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

The 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

When you have become familiar with Bash, why not compare it with another shell by simply running the new shell from the command line? For example, explore the Z shell by issuing the following command.

$ zsh


Type exit when you want to exit Zsh and return to Bash.


Learn More

Project 4 covers Bash basics and customization.


Learn More

The projects in Chapter 6 help you make the most of your command-line experience.


Command-Line Editing

You'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:

  • Control-a to move to the start of the line

  • Control-e to move to the end of the line

  • Control-k to delete from the cursor to the end of line

  • Control-u to delete from the cursor to the start of the line




Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

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