CARVIEW |
Select Language
HTTP/2 302
server: nginx
date: Thu, 21 Aug 2025 12:10:33 GMT
content-type: text/plain; charset=utf-8
content-length: 0
x-archive-redirect-reason: found capture at 20071125182946
location: https://web.archive.org/web/20071125182946/https://www.oreilly.com/catalog/lunixmacosx/toc.html
server-timing: captures_list;dur=0.551425, exclusion.robots;dur=0.022450, exclusion.robots.policy;dur=0.010626, esindex;dur=0.012260, cdx.remote;dur=7.391501, LoadShardBlock;dur=166.356979, PetaboxLoader3.datanode;dur=62.624862, PetaboxLoader3.resolve;dur=46.238132
x-app-server: wwwb-app216
x-ts: 302
x-tr: 205
server-timing: TR;dur=0,Tw;dur=0,Tc;dur=0
set-cookie: wb-p-SERVER=wwwb-app216; path=/
x-location: All
x-rl: 0
x-na: 0
x-page-cache: MISS
server-timing: MISS
x-nid: DigitalOcean
referrer-policy: no-referrer-when-downgrade
permissions-policy: interest-cohort=()
HTTP/2 200
server: nginx
date: Thu, 21 Aug 2025 12:10:34 GMT
content-type: text/html
x-archive-orig-date: Sun, 25 Nov 2007 18:29:46 GMT
x-archive-orig-server: Apache
x-archive-orig-p3p: policyref="https://www.oreillynet.com/w3c/p3p.xml",CP="CAO DSP COR CURa ADMa DEVa TAIa PSAa PSDa IVAa IVDa CONo OUR DELa PUBi OTRa IND PHY ONL UNI PUR COM NAV INT DEM CNT STA PRE"
x-archive-orig-last-modified: Fri, 16 Nov 2007 10:33:14 GMT
x-archive-orig-etag: "6dc205-35be7-473d71ea"
x-archive-orig-accept-ranges: bytes
x-archive-orig-content-length: 220135
x-archive-orig-x-cache: MISS from oregano.bp
x-archive-orig-x-cache-lookup: MISS from oregano.bp:3128
x-archive-orig-via: 1.0 oregano.bp:3128 (squid/2.6.STABLE12)
x-archive-orig-connection: close
x-archive-guessed-content-type: text/html
x-archive-guessed-charset: iso-8859-1
memento-datetime: Sun, 25 Nov 2007 18:29:46 GMT
link: ; rel="original", ; rel="timemap"; type="application/link-format", ; rel="timegate", ; rel="first memento"; datetime="Wed, 12 Jun 2002 13:51:03 GMT", ; rel="prev memento"; datetime="Mon, 01 Jan 2007 14:02:11 GMT", ; rel="memento"; datetime="Sun, 25 Nov 2007 18:29:46 GMT", ; rel="last memento"; datetime="Sun, 25 Nov 2007 18:29:46 GMT"
content-security-policy: default-src 'self' 'unsafe-eval' 'unsafe-inline' data: blob: archive.org web.archive.org web-static.archive.org wayback-api.archive.org athena.archive.org analytics.archive.org pragma.archivelab.org wwwb-events.archive.org
x-archive-src: 52_0_20071125180547_crawl100-c/52_0_20071125182613_crawl100.arc.gz
server-timing: captures_list;dur=0.552928, exclusion.robots;dur=0.019290, exclusion.robots.policy;dur=0.008760, esindex;dur=0.013171, cdx.remote;dur=32.327851, LoadShardBlock;dur=141.192233, PetaboxLoader3.datanode;dur=207.371751, load_resource;dur=301.080348, PetaboxLoader3.resolve;dur=207.037708
x-app-server: wwwb-app216
x-ts: 200
x-tr: 613
server-timing: TR;dur=0,Tw;dur=0,Tc;dur=0
x-location: All
x-rl: 0
x-na: 0
x-page-cache: MISS
server-timing: MISS
x-nid: DigitalOcean
referrer-policy: no-referrer-when-downgrade
permissions-policy: interest-cohort=()
content-encoding: gzip
oreilly.com -- Online Catalog: Learning Unix for Mac OS X
Buy this Book
PDF Chapter
$3.99
Read it Now!
Reprint Licensing


--
Please select a chapter from the Table of Contents and click the button above to begin the licensing process.
Table of Contents
- Chapter 1: Getting Started
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterWith a typical Unix system, a staff person has to set up a Unix account for you before you can use it. With Mac OS X, however, every install automatically creates a default user account. The account is identified by your username , which is usually a single word or an abbreviation. Think of this account as your office—it's your personal place in the Unix environment.When you log in to your OS X system, you're automatically logged into your Unix account as well. In fact, your Desktop and other customized features of your OS X environment have corresponding features in the Unix environment. Your files and programs can be accessed either through the Mac Finder or through a variety of Unix command-line utilities that you can reach from within OS X's Terminal window.To get into the Unix environment, launch the Terminal application. (That's Finder → Applications → Utilities → Terminal. If you expect to use the Terminal a lot, drag the Terminal icon from the Finder window onto the Dock. You can then launch Terminal with a single click.) Once Terminal is running, you'll see a window like the one in Figure 1-1.Figure 1-1: The Terminal windowOnce you have a window open and you're typing commands, it's helpful to know that regular Mac OS X cut and paste commands work, so it's simple to send an email message to a colleague showing your latest Unix interaction, or to paste some text from a web page into a file you're editing with a Unix text editor such as vi.You can also have a number of different Terminal windows open if that helps your workflow. Simply use
-N to open each one, and
-~ to cycle between them without removing your hands from the keyboard.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Working in the Unix Environment
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterTo get into the Unix environment, launch the Terminal application. (That's Finder → Applications → Utilities → Terminal. If you expect to use the Terminal a lot, drag the Terminal icon from the Finder window onto the Dock. You can then launch Terminal with a single click.) Once Terminal is running, you'll see a window like the one in Figure 1-1.Figure 1-1: The Terminal windowOnce you have a window open and you're typing commands, it's helpful to know that regular Mac OS X cut and paste commands work, so it's simple to send an email message to a colleague showing your latest Unix interaction, or to paste some text from a web page into a file you're editing with a Unix text editor such as vi.You can also have a number of different Terminal windows open if that helps your workflow. Simply use
-N to open each one, and
-~ to cycle between them without removing your hands from the keyboard.
If you have material in your scroll buffer you want to find, use-F (select Find Panel from the Edit menu) and enter the specific text.
-G (Find Next) lets you search down the scroll buffer for the next occurrence, and
-D (Find Previous) lets you search up the scroll buffer for the previous occurrence. You can also accomplish this by highlighting a passage, entering
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Syntax of Unix Command Lines
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterUnix command lines can be simple, one-word entries such as the
date
command. They can also be more complex; you may need to type more than the command or program name.A Unix command can have arguments. An argument can be an option or a filename. The general format for a Unix command line is:command option(s) filename(s)
There isn't a single set of rules for writing Unix commands and arguments, but these general rules work in most cases:-
Enter commands in lowercase.
-
Options modify the way in which a command works. Options are often single letters prefixed with a dash (
-
, also called "hyphen" or "minus") and set off by any number of spaces or tabs. Multiple options in one command line can be set off individually (such as-a -b
). In some cases, you can combine them after a single dash (such as-ab
), but most commands' documentation doesn't tell you whether this will work; you'll have to try it.Some commands also have options made from complete words or phrases and starting with two dashes, such as--delete
or--confirm-delete
. When you enter a command line, you can use this option style, the single-letter options (which each start with a single dash), or both. -
The argument filename is the name of a file you want to use. Most Unix programs also accept multiple filenames, separated by spaces or specified with wildcards (see Chapter 3). If you don't enter a filename correctly, you may get a response such as "filename: no such file or directory" or "filename: cannot open."Some commands, such as
telnet
andwho
(shown earlier in this chapter), have arguments that aren't filenames. -
You must type spaces between commands, options, and filenames. You'll need to "quote " filenames that contain spaces. For more information, see Section 3.1.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! -
- Types of Commands
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterWhen you use a program, you'll want to know how to control it. How can you tell it what job you want done? Do you give instructions before the program starts, or after it's started? There are several general ways to give commands on a Unix system. It's good to be aware of them.
-
Some programs work only within the graphical window environment (on Mac OS X, this is called Aqua). On Mac OS X, you can run these programs using the
open
command. For instance, when you typeopen /Applications/Chess.app
at a shell prompt, the chess game starts. It opens one or more windows on your screen. The program has its own way to receive your commands—through menus and buttons on its windows, for instance. -
You've also seen in Section 1.2, that you can enter many Unix commands at a shell prompt. These programs work in a window system (from a terminal window) or from any terminal. You control those programs from the Unix command line—that is, by typing options and arguments from a shell prompt before you start the program. After you start the program, wait for it to finish; you generally don't interact with it.
-
Some Unix programs that work in the terminal window have commands of their own. (If you'd like some examples, see Chapter 2 and Chapter 3.) These programs may accept options and arguments on their command lines. But, once you start a program, it prints its own prompt and/or menus, and it understands its own commands; it takes instructions from your keyboard that weren't given on its command line.For instance, if you enter
ftp
at a shell prompt, you'll see a new prompt from theftp
program. Enter FTP commands to transfer files to and from remote systems. When you enter the special commandquit
to quit theftp
program,ftp
will stop prompting you. Then you'll get another shell prompt, where you can enter other Unix commands.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! -
- The Unresponsive Terminal
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterDuring your Unix session, your terminal may not respond when you type a command, or the display on your screen may stop at an unusual place. That's called a "hung" or "frozen" terminal or session. Note that most of the techniques in this section apply to a terminal window, but not to non-terminal windows such as a web browser.A session can hang for several reasons. For instance, your computer can get too busy; the Terminal application has to wait its turn. In that case, your session starts by itself after a few moments. You should not try to "un-hang" the session by entering extra commands, because those commands will all take effect after Terminal comes back to life.If the system doesn't respond for quite a while (how long that is depends on your individual situation; ask other users about their experiences), the following solutions usually work. Try the following steps in the order shown until the system responds:
-
Press the Return key once.You may have typed text at a prompt (for example, a command line at a shell prompt) but haven't yet pressed Return to say that you're done typing and your text should be interpreted.
-
Try job control (see Chapter 9); type Control-Z.This control key sequence suspends a program that may be running and gives you a shell prompt. Now you can enter the
jobs
command to find the program's name, then restart the program withfg
or terminate it withkill
. -
Use your interrupt key (found earlier in this chapter in Section 1.1.4; typically Control-C).This interrupts a program that may be running. (Unless the program is run in the background, as described in Section 9.1, the shell waits for it to finish before giving a new prompt. A long-running program may thus appear to hang the terminal.) If this doesn't work the first time, try it once more; doing it more than twice usually won't help.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! -
- Chapter 2: Using Unix
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterOnce you launch Terminal, you can use the many facilities that Unix provides. As an authorized system user, you have an account that gives you:
-
A place in the Unix filesystem where you can store your files.
-
A username that identifies you, lets you control access to your files, and is an address for your email (although it may not be your main address).
-
An environment you can customize.
A file is the unit of storage in Unix, as it is in the Mac environment. A file can hold anything: text (a report you're writing, a to-do list), a program, digitally encoded pictures or sound, and so on. All of those are just sequences of raw data until they're interpreted by the right program.In Unix, files are organized into directories. A directory is actually a special kind of file where the system stores information about other files. (A Unix directory is identical to a Mac folder.) You can think of a directory as a place, so that files are said to be contained in directories, and you work inside a directory.This section introduces the Unix filesystem. Later sections show how you can look in files and protect them. Chapter 3 has more information.When you launch Terminal, you're placed in a directory called your home directory. This directory, a unique place in the Mac OS X filesystem, contains the files you use almost every time you log in. In your home directory, you can create your own files. As you'll see, you can also store your own directories within your home directory. Like folders in a file cabinet, this is a good way to organize your files.YourAdditional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! -
- The Unix Filesystem
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterA file is the unit of storage in Unix, as it is in the Mac environment. A file can hold anything: text (a report you're writing, a to-do list), a program, digitally encoded pictures or sound, and so on. All of those are just sequences of raw data until they're interpreted by the right program.In Unix, files are organized into directories. A directory is actually a special kind of file where the system stores information about other files. (A Unix directory is identical to a Mac folder.) You can think of a directory as a place, so that files are said to be contained in directories, and you work inside a directory.This section introduces the Unix filesystem. Later sections show how you can look in files and protect them. Chapter 3 has more information.When you launch Terminal, you're placed in a directory called your home directory. This directory, a unique place in the Mac OS X filesystem, contains the files you use almost every time you log in. In your home directory, you can create your own files. As you'll see, you can also store your own directories within your home directory. Like folders in a file cabinet, this is a good way to organize your files.Your working directory (also called your current directory) is the directory in which you're currently working. Every time you launch Terminal, your home directory is your working directory. When you change to another directory, the directory you move to becomes your working directory.Unless you tell Unix otherwise, all commands that you enter apply to the files in your working directory. In the same way, when you create files, they're created in your working directory unless you specify another directory. For instance, if you type the commandAdditional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Looking Inside Files with less
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterBy now , you're probably tired of looking at files from the outside. It's akin to visiting a bookstore and looking at the covers, but never getting to read a word. Let's look at a program for reading text files.If you want to "read" a long file on the screen, you can use the
less
command to display one "page" (a Terminal window filled from top to bottom) of text at a time.If you don't likeless
, you can try a very similar program namedmore
. (In fact, the nameless
is a play on the name ofmore
, which came first.) The syntax forless
is:less option(s) file(s)
less
lets you move forward or backward in the files by any number of pages or lines; you can also move back and forth between two or more files specified on the command line. When you invokeless
, the first "page" of the file appears. A prompt appears at the bottom of the Terminal window, as in the following example:% less ch03 A file is the unit of storage in Unix, as in most other systems. A file can hold anything: text (a report you're writing, . . . :
The basicless
prompt is a colon (:
); although, for the first screenful,less
displays the file's name as a prompt. The cursor sits to the right of this prompt as a signal for you to enter aless
command to tellless
what to do. To quit, typeq
.Like almost everything aboutless
, the prompt can be customized. For example, using the-M
starting flag on theless
command line makes the prompt show the filename and your position in the file (as a percentage). If you want this to happen every time you useless
, you can set theLESS
environment variable toM
(without a dash) in your shell setup file. See Section 4.2.You can set or unset most options temporarily from theless
prompt. For instance, if you have the shortless
prompt (a colon), you can enter-M
whileless
is running.less
responds "Long prompt (press Return)," and for the rest of the session,Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Protecting and Sharing Files
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterUnix makes it easy for users on the same system to share files and directories. For instance, everyone in a group can read documents stored in one of their manager's directories without needing to make their own copies, if the manager has allowed access. There might be no need to fill peoples' email inboxes with file attachments if everyone can access those files directly through the Unix filesystem.Here's a brief introduction to file security and sharing. If you have critical security needs, or you just want more information, talk to your system staff or see an up-to-date book on Unix security such as Practical Unix and Internet Security (O'Reilly).Note that the system's superuser (the system administrator and possibly other users) can do anything to any file at any time, no matter what its permissions are. So, access permissions won't keep your private information safe from everyone—although let's hope that you can trust your system administrator!Your system administrator should also keep backup copies of users' files. These backup copies may be readable by anyone who has physical access to them. That is, anyone who can take the backup out of a cabinet (or wherever) and mount it on a computer system may be able to read the file copies. The same is true for files stored on floppy disks and any other removable media. (Once you take a file off a Unix system, that system can't control access to it anymore.)A directory's access permissions help to control access to the files and subdirectories in that directory:
-
If a directory has read permission, a user can run
ls
to see what's in the directory and use wildcards to match files in it. -
A directory that has write permission allows users to add, rename, and delete files in the directory.
-
To access a directory (that is, to read or write the files in the directory or to run the files if they're programs) a user needs execute permission on that directory. Note that to access a directory, a user must
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! -
- Graphical Filesystem Browsers
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterBecause you've the luxury of running Unix within the Mac OS X environment, there's also a terrific graphical way to do some of the things you can do with files from the command line. A filesystem browser , such as the Finder, lets you see a graphical representation of the filesystem and do a limited number of operations on it. Figure 2-6 shows the Finder in its default icon view. Other views that are helpful are listing and directory views, each offering more information about the directories above and below the current directory.Figure 2-6: Mac OS X Finder, icon viewThe Finder can be handy for seeing what's in the filesystem. Unfortunately, because the Finder takes you away from the shell you're using for other work, it can limit what you're able to do with Unix. (You'll see additional information about why this is true when we cover more advanced features such as input/output redirection in Section 6.1.1.) We recommend learning more about the Finder, but also learning what you can do at the more powerful Unix command line.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Completing File and Directory Names
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterMost Unix shells can complete a partly typed file or directory name for you. Different shells have different methods. If you're using the default shell in Mac OS X, tcsh, just type the first few letters of the name, then press Tab. If the shell can find just one way to finish the name, it will; your cursor will move to the end of the new name, where you can type more or press Return to run the command. (You can also edit or erase the completed name.)What happens if more than one file or directory name matches what you've typed so far? You will get a list of all possible completions; try pressing Tab and you may see a list of all names starting with the characters you've typed so far. Here's an example from the tcsh shell:
% maTab mach_init mailq make makemap man machine mailstat makedbm makepsres mail mailstats makedepend malloc_history % ma
At this point, you could type another character or two—ani
, for example—and then press Tab once more to list only the mail-related commands.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Chapter 3: File Management
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterChapter 2 introduced the Unix filesystem. This chapter explains how to name, edit, copy, move, and find files.As Chapter 2 explains, both files and directories are identified by their names. A directory is really just a special kind of file, so the rules for naming directories are the same as the rules for naming files.Filenames may contain any character except
/
, which is reserved as the separator between files and directories in a pathname. Filenames are usually made of upper- and lowercase letters, numbers, "." (dots), and "_" (underscores). Other characters (including spaces) are legal in a filename, but they can be hard to use because the shell gives them special meanings. However, spaces are a standard part of Macintosh file and folder names, so while we recommend using only letters, numbers, dots, and underscore characters for filenames, the reality is that you will have to figure out how to work with the spaces in file and directory names. The Finder, by contrast, dislikes colons (which it uses as a directory separator, just as Unix uses the slash). If you display a file called test:me in the Finder, the name is shown as test/me instead.If you have a file with spaces in its name, the shell will be confused if you type its name on the command line. That's because the shell breaks command lines into separate arguments at the spaces. To tell the shell not to break an argument at spaces, either put quotation marks ("
) around the argument or preface each space with a backslash (\
).For example, therm
program, covered later in this chapter, removes Unix files. To remove a file named a confusing name, the firstrm
command in the following snippet doesn't work, but the second one does. Also note that you can escape spaces (that is, avoid having the shell interpret them inappropriately) by using a backslash character, as shown in the third example:% ls -l total 2 -rw-r--r-- 1 taylor staff 324 Feb 4 23:07 a confusing name -rw-r--r-- 1 taylor staff 64 Feb 4 23:07 another odd name %
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - File and Directory Names
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterAs Chapter 2 explains, both files and directories are identified by their names. A directory is really just a special kind of file, so the rules for naming directories are the same as the rules for naming files.Filenames may contain any character except
/
, which is reserved as the separator between files and directories in a pathname. Filenames are usually made of upper- and lowercase letters, numbers, "." (dots), and "_" (underscores). Other characters (including spaces) are legal in a filename, but they can be hard to use because the shell gives them special meanings. However, spaces are a standard part of Macintosh file and folder names, so while we recommend using only letters, numbers, dots, and underscore characters for filenames, the reality is that you will have to figure out how to work with the spaces in file and directory names. The Finder, by contrast, dislikes colons (which it uses as a directory separator, just as Unix uses the slash). If you display a file called test:me in the Finder, the name is shown as test/me instead.If you have a file with spaces in its name, the shell will be confused if you type its name on the command line. That's because the shell breaks command lines into separate arguments at the spaces. To tell the shell not to break an argument at spaces, either put quotation marks ("
) around the argument or preface each space with a backslash (\
).For example, therm
program, covered later in this chapter, removes Unix files. To remove a file named a confusing name, the firstrm
command in the following snippet doesn't work, but the second one does. Also note that you can escape spaces (that is, avoid having the shell interpret them inappropriately) by using a backslash character, as shown in the third example:% ls -l total 2 -rw-r--r-- 1 taylor staff 324 Feb 4 23:07 a confusing name -rw-r--r-- 1 taylor staff 64 Feb 4 23:07 another odd name % rm a confusing name rm: a: no such file or directory rm: confusing: no such file or directory rm: name: no such file or directory %
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - File and Directory Wildcards
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterWhen you have a number of files named in series (for example, chap1 to chap12) or filenames with common characters (such as aegis, aeon, and aerie), you can use wildcards to specify many files at once. These special characters are
*
(asterisk),?
(question mark), and[
]
(square brackets). When used in a file or directory name given as an argument on a command line, the following is true:-
*
-
An asterisk stands for any number of characters in a filename. For example,
ae*
would match aegis, aerie, aeon, etc. if those files were in the same directory. You can use this to save typing for a single filename (for example,al*
for alphabet.txt) or to choose many files at once (as inae*
). A*
by itself matches all file and subdirectory names in a directory, with the exception of any starting with a period. To match all your dot files, try.??*
. -
?
-
A question mark stands for any single character (so
h?p
matches hop and hip, but not help). -
[]
-
Square brackets can surround a choice of single characters (i.e., one digit or one letter) you'd like to match. For example,
[Cc]hapter
would match either Chapter or chapter, butchap[12]
would match chap1 or chap2. Use a hyphen (-
) to separate a range of consecutive characters. For example,chap[1-3]
would match chap1, chap2, or chap3.
The following examples show the use of wildcards. The first command lists all the entries in a directory, and the rest use wildcards to list just some of the entries. The last one is a little tricky; it matches files whose names contain two (or more) a's.% ls chap10 chap2 chap5 cold chap1a.old chap3.old chap6 haha chap1b chap4 chap7 oldjunk %
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! -
- Creating and Editing Files
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterOne easy way to create a file is with a Unix feature called input/output redirection, as Chapter 5 explains. This sends the output of a program directly to a file, to make a new file or add to an existing one.You'll usually create and edit a plain-text file with a text editor program. Text editors are somewhat different than word processors.A text editor lets you add, change, and rearrange text easily. Three popular Unix editors included with Mac OS X are vi (pronounced "vee-eye"), Pico ("pea-co"), and Emacs ("e-max").Since there are several editor programs, you can choose one you're comfortable with. vi is probably the best choice because almost all Unix systems have it, but Emacs is also widely available. If you'll be doing simple editing only, Pico is a great choice. Although Pico is much less powerful than Emacs or vi, it's also a lot easier to learn. For this book, however, we'll focus on the rudiments of vi as it's the most widely available Unix editor, and there's a version of vi included with Mac OS X.None of those editors has the same features as popular word-processing software within the graphical face of Mac OS X, but vi and Emacs are sophisticated, extremely flexible editors for all kinds of plain-text files: programs, email messages, and so on. Of course, you can opt to use an Aqua-based editor such as BBEdit or TextEdit with good results too, if you'd rather just sidestep editing while within the Terminal application. If you do, try using the
open
command within the Terminal to launch the editor with the proper file already loaded. For example:open -e myfile.txt
.The only caveat regarding switching between Aqua applications and Unix applications for editing is that you might end up having to translate file formats along the way. Fortunately, this is easy with Unix.One of the more awkward things about Apple putting a Mac graphical environment on top of a Unix core is that the two systems use different end-of-line character sequences. If you ever open up a file in an Aqua application and see lots of little boxes at the end of each line, or if you try to edit a file within Unix and find that it's littered withAdditional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Managing Your Files
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterThe tree structure of the Unix filesystem makes it easy to organize your files. After you make and edit some files, you may want to copy or move files from one directory to another, or rename files to distinguish different versions of a file. You may want to create new directories each time you start a different project. If you copy a file, it's worth learning about the subtle sophistication of the
cp
andCpMac
commands: if you copy a file to a directory, it automatically reuses the filename in the new location. This can save lots of typing!A directory tree can get cluttered with old files you don't need. If you don't need a file or a directory, delete it to free storage space on the disk. The following sections explain how to make and remove directories and files.It's handy to group related files in the same directory. If you were writing a spy novel, you probably wouldn't want your intriguing files mixed with restaurant listings. You could create two directories: one for all the chapters in your novel (spy, for example), and another for restaurants (boston.dine).To create a new directory, use themkdir
program. The syntax is:mkdir dirname(s)
dirname is the name of the new directory. To make several directories, put a space between each directory name. To continue our example, you would enter:% mkdir spy boston.dine
If you're about to edit a file, you may want to save a copy first. That makes it easy to get back the original version. You should use thecp
program when copying plain files and directories. All other Macintosh files (that is, those with resource forks) should be copied withCpMac
(available only if you have installed Apple's Mac OS X developer CD).Section 3.4.2.1: cp
Thecp
program can put a copy of a file into the same directory or into another directory.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Chapter 4: Customizing Your Session
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterOne of the great pleasures of using Unix with Mac OS X surrounding it is that you get the benefit of a truly wonderful graphical application environment and the underlying power of the raw Unix interface. A match made in heaven!This chapter discusses how to customize your Terminal environment both from the graphical user interface using Terminal → Preferences and from the Unix shell by using shell configuration files.Launch Terminal and you have a dull, uninspiring white window with black text that says "Welcome to Darwin!" and a shell prompt. But that's okay. We can fix it.To change the display preferences in the Terminal application, go to the Terminal menu and choose Preferences.... You see a display similar to Figure 4-1.Figure 4-1: Startup PreferencesAlong the window's top, notice that a number of different preferences are configurable: Startup, Shell, Window, Text & Colors, Buffer, Emulation, and Activity. The icons suggest what each does, but let's have a closer look anyway, particularly since some of these settings definitely should be changed in our view. It's worth pointing out that these changes affect new windows not the current window.
Section 4.1.1.1: Startup Preferences
When you first open Terminal Preferences, the Startup Preferences are displayed, as shown in Figure 4-1. The default behavior is to launch a new blank shell window each time the program is started, so you're ready to start typing commands immediately. Unless you're an advanced user, don't change this behavior. Instead, let's look at what's on the other preference screens, which you can view by clicking on each icon at the top of the Preferences window.Section 4.1.1.2: Shell Preferences
As Figure 4-2 reveals, there are a number of behavioral choices you can make on this panel, including whether you want each window to have its own login shell associated with your account, or whether all Terminal windows should run a specified shell (in this case,Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Launching Terminal
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterLaunch Terminal and you have a dull, uninspiring white window with black text that says "Welcome to Darwin!" and a shell prompt. But that's okay. We can fix it.To change the display preferences in the Terminal application, go to the Terminal menu and choose Preferences.... You see a display similar to Figure 4-1.Figure 4-1: Startup PreferencesAlong the window's top, notice that a number of different preferences are configurable: Startup, Shell, Window, Text & Colors, Buffer, Emulation, and Activity. The icons suggest what each does, but let's have a closer look anyway, particularly since some of these settings definitely should be changed in our view. It's worth pointing out that these changes affect new windows not the current window.
Section 4.1.1.1: Startup Preferences
When you first open Terminal Preferences, the Startup Preferences are displayed, as shown in Figure 4-1. The default behavior is to launch a new blank shell window each time the program is started, so you're ready to start typing commands immediately. Unless you're an advanced user, don't change this behavior. Instead, let's look at what's on the other preference screens, which you can view by clicking on each icon at the top of the Preferences window.Section 4.1.1.2: Shell Preferences
As Figure 4-2 reveals, there are a number of behavioral choices you can make on this panel, including whether you want each window to have its own login shell associated with your account, or whether all Terminal windows should run a specified shell (in this case, /bin/tcsh). In Mac OS X you're probably the only person who uses your computer, so if your login shell is acceptable to you, leaving the shell setting to reference the login shell is your best bet.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Customizing Your Shell Environment
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterThe Unix shell reads a number of configuration files when it starts up. These configuration files are really shell programs , so they are extraordinarily powerful. Shell programming is beyond the scope of this book. For more detail, see Paul DuBois' book, Using csh and tcsh (O'Reilly). Because Unix is a multiuser system, there are two possible locations for the configuration files: one applies to all users of the system and another to each individual user.The system-wide setup files that are read by tcsh, the default shell for Mac OS X, are found in /usr/share/init/tcsh. You only have permission to change these system-wide files if you are logged in as root. However, you can create an additional file called .tcshrc in your home directory that will add additional commands to be executed whenever you start a new Terminal window. (If you configure Terminal to use another shell, such as the Bourne shell, the C shell, or the Z shell, you'll need to set up different configuration files, which we don't discuss.) The system-wide setup files are read first, then the user-specific ones, so commands in your .tcshrc file may override those in the system-wide files.The .tcshrc file can contain any shell command that you want to run automatically whenever you create a new Terminal. Some typical examples include changing the shell prompt, setting environment variables (values that control the operation of other Unix utilities), setting aliases, or adding to the search path (where the shell searches for programs to be run). A .tcshrc file could look like this:
set prompt="%/ %h% " setenv LESS 'eMq' alias desktop "cd /Users/taylor/Desktop" date
This sample .tcshrc file issues the following commands:-
The line with
set
prompt
tells the shell to use a different prompt than the standard one. We'll explain the details of prompt setting in Section 4.2.1 later in this chapter. -
The line with
setenv
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! -
- Further Customization
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterThere's not much more you can do with the Terminal application than what's shown in this chapter, but there's an infinite amount of customization possible with the tcsh shell (or any other shell you might have picked). To learn more about how to customize your shell, read the manpage. Be warned, though, the tcsh manpage is over 5,800 lines long!Oh, and in case you're wondering, manpages are the Unix version of online help documentation. Just about every command-line (Unix) command has a corresponding manpage with lots of information on starting flags, behaviors, and much more. You can access any manpage by simply typing
man
cmd. Start withman man
to learn more about the man system.For more information on customizing tcsh, see Paul DuBois' book, Using csh and tcsh, or Unix Power Tools, Second Edition , by Jerry Peek, Tim O'Reilly, and Mike Loukides, both available from O'Reilly.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Chapter 5: Printing
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterWorking in the Macintosh environment, you're used to a simple and elegant printer interface, particularly in OS X, where Print Center makes it a breeze to add new printers and configure your existing printers. The Unix environment has never had a printing interface that even comes close in usability, and while the standard print command in Unix is
lpr
, getting it to work on OS X involves reconfiguring your system and a number of tricky system administration tasks best avoided if you're not a Unix expert.If you do want to try, see Configuring Your Printer at the end of this chapter for some suggestions on how to proceed.The good news is that Apple has included a couple of alternative command-line interfaces to printers, notablyatprint
for AppleTalk-based printers, andPrint
, a program that's supposed to inject your print jobs into the regular Aqua print queue.Regardless of what program you're going to use for printing, before you print a file on a Unix system, you may want to reformat it to adjust the margins, highlight some words, and so on. Most files can also be printed without reformatting, but the raw printout might not look quite as nice. Further, some printers accept only PostScript, which means you'll need to use a text-to-PostScript filter such asenscript
for good results. Before we cover printing itself, let's look at bothpr
andenscript
to see how they work.Thepr
program does minor formatting of files on the terminal screen or for a printer. For example, if you have a long list of names in a file, you can format it onscreen into two or more columns.The syntax is:pr option(s) filename(s)
pr
changes the format of the file only on the screen or on the printed copy; it doesn't modify the original file. Table 5-1 lists somepr
options.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Formatting and Print Commands
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterThe good news is that Apple has included a couple of alternative command-line interfaces to printers, notably
atprint
for AppleTalk-based printers, andPrint
, a program that's supposed to inject your print jobs into the regular Aqua print queue.Regardless of what program you're going to use for printing, before you print a file on a Unix system, you may want to reformat it to adjust the margins, highlight some words, and so on. Most files can also be printed without reformatting, but the raw printout might not look quite as nice. Further, some printers accept only PostScript, which means you'll need to use a text-to-PostScript filter such asenscript
for good results. Before we cover printing itself, let's look at bothpr
andenscript
to see how they work.Thepr
program does minor formatting of files on the terminal screen or for a printer. For example, if you have a long list of names in a file, you can format it onscreen into two or more columns.The syntax is:pr option(s) filename(s)
pr
changes the format of the file only on the screen or on the printed copy; it doesn't modify the original file. Table 5-1 lists somepr
options.Table 5-1: Some pr options OptionDescription-k
Producesk
columns of output-d
Double-spaces the output (may not work on all versions ofpr
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Configuring Your LPR Printer
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterWhile there are a great many areas of Mac OS X that are enhanced by the addition of Unix as the low-level operating system, one area has become more complex: printing. If you only live in the world of Aqua, it's not too bad because Print Center manages all your needs, but if you want to print from the Unix command line and you don't have an AppleTalk printer, you've got a bit of a tinkering job ahead of you.If you do have a printer accessible through AppleTalk, flip back a few pages and read the section about the
atprint
command, or useman atprint
to learn how to simplify your life considerably.Otherwise, configuring your printer requires four steps: adding an entry to /etc/printcap for the printer, creating a spool directory and log file skeleton, importing the printcap entry into NetInfo, and adding the printer to Print Center.To do this, you'll need both your Unix root password and your system administrator password. If you're unsure about your Unix root password, you can go into NetInfo and search the help system.The first step is to edit the printer capabilities database /etc/printcap. You need to be root to do this, so it's easiest to use thesudo
command to simply run theedit
command as root:% sudo vi /etc/printcap
This file contains lots of cryptic information that is attempting to define the capabilities and interface for each known printer accessible from Unix. The default entry in the file is for a local printer that's accessible through the /dev/lp device:lp|local line printer:\ :lp=/dev/lp:sd=/var/spool/output/lpd:lf=/var/log/lpd-errs:
Odds are that this printer configuration is not going to work for you, however, so comment it out and add a new one for the new printer. In this example, 198.76.82.151 is the IP address of an HP LaserJet 2100TN Ethernet printer, which we'll call lj for the Unix system. If your printer has a unique hostname, that's better to use than the IP address, but either way, carefully duplicate the following:Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Chapter 6: Redirecting I/O
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterMany Unix programs read input (such as a file) and write output. In this chapter, we discuss Unix programs that handle their input and output in a standard way. This lets them work with each other.This chapter generally doesn't apply to full-screen programs, such as the Pico editor, that take control of your whole Terminal window. (The pager programs,
less
, andmore
do work together in this way.) It also doesn't apply to graphical programs, such as the Finder or Internet Explorer, that open their own windows on your screen.What happens if you don't give a filename argument on a command line? Most programs will take their input from your keyboard instead (after you press Return to start the program running, that is). Your Terminal keyboard is the program's standard input.As a program runs, the results are usually displayed on your Terminal screen. The Terminal screen is the program's standard output . So, by default, each of these programs takes its information from the standard input and sends the results to the standard output. These two default cases of input/output (I/O) can be varied. This is called I/O redirection .If a program doesn't normally read from files, but reads from its standard input, you can give a filename by using the<
(less-than symbol) operator. For example, themail
program (see Section 8.2.3 in Chapter 8) normally reads the message to send from your keyboard. Here's how to use the input redirection operator to count the number of lines in the file to_do :% wc -l < to_do %
If a program writes to its standard output, which is normally the screen, you can make it write to a file instead by using the greater-than symbol (>
) operator. The pipe operator (|
) sends the standard output of one program to the standard input of another program. Input/output redirection is one of the most powerful and flexible Unix features.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Standard Input and Standard Output
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterWhat happens if you don't give a filename argument on a command line? Most programs will take their input from your keyboard instead (after you press Return to start the program running, that is). Your Terminal keyboard is the program's standard input.As a program runs, the results are usually displayed on your Terminal screen. The Terminal screen is the program's standard output . So, by default, each of these programs takes its information from the standard input and sends the results to the standard output. These two default cases of input/output (I/O) can be varied. This is called I/O redirection .If a program doesn't normally read from files, but reads from its standard input, you can give a filename by using the
<
(less-than symbol) operator. For example, themail
program (see Section 8.2.3 in Chapter 8) normally reads the message to send from your keyboard. Here's how to use the input redirection operator to count the number of lines in the file to_do :% wc -l < to_do %
If a program writes to its standard output, which is normally the screen, you can make it write to a file instead by using the greater-than symbol (>
) operator. The pipe operator (|
) sends the standard output of one program to the standard input of another program. Input/output redirection is one of the most powerful and flexible Unix features.Instead of always letting a program's output come to the screen, you can redirect output to a file. This is useful when you'd like to save program output, or when you put files together to make a bigger file.Section 6.1.1.1: cat
cat
, which is short for "concatenate," reads files and outputs their contents one after another, without stopping.To display files on the standard output (your screen), use:Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Pipes and Filters
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterWe've seen how to redirect input from a file and output to a file. You can also connect two programs together so that the output from one program becomes the input of the next program. Two or more programs connected in this way form a pipe. To make a pipe , put a vertical bar (
|
) on the command line between two commands. When a pipe is set up between two commands, the standard output of the command to the left of the pipe symbol becomes the standard input of the command to the right of the pipe symbol. Any two commands can form a pipe as long as the first program writes to standard output and the second program reads from standard input.When a program takes its input from another program, performs some operation on that input, and writes the result to the standard output (which may be piped to yet another program), it is referred to as a filter. A common use of filters is to modify output. Just as a common filter culls unwanted items, Unix filters can restructure output.Most Unix programs can be used to form pipes. Some programs that are commonly used as filters are described in the next sections. Note that these programs aren't used only as filters or parts of pipes. They're also useful on their own.Thegrep
program searches a file or files for lines that have a certain pattern. The syntax is:grep "pattern" file(s)
The name "grep" is derived from theed
(a Unix line editor) commandg/re/p
, which means "globally search for a regular expression and print all matching lines containing it." A regular expression is either some plain text (a word, for example) and/or special characters used for pattern matching. When you learn more about regular expressions, you can use them to specify complex patterns of text.The simplest use ofgrep
is to look for a pattern consisting of a single word. It can be used in a pipe so only those lines of the input files containing a given string are sent to the standard output. But let's start with an example reading from files: searching all files in the working directory for a word—say,Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Chapter 7: Accessing the Internet
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterA network lets computers communicate with each other, sharing files, email, and much more. Unix systems have been networked for more than 25 years, and Macintosh systems have always had networking as an integral part of the system design from the very first system released in 1984.This chapter introduces Unix networking: running programs on other computers, copying files between computers, browsing the World Wide Web, sending and receiving email messages, reading and posting messages to Usenet "Net news" groups, and "chatting" interactively with other users on your local computer or worldwide.The computer you log in to may not be the computer you need to use. For instance, you might have a nifty iMac running Mac OS X on your desk but need to do some work on the main computer in another building. Or you might be a professor doing research with a computer at another university. Your Mac can connect to another Unix computer to let you work as if you were sitting at that computer. You can actually connect to another Mac running Mac OS X (if enabled) too, but you can't run Aqua applications, just the command line. This section describes how to connect to another computer from within the Terminal.If you'd like to set up your computer to allow remote logins, your best bet is to choose System Preferences from the Mac OS X Apple menu in the top left corner of your screen. Within the Sharing pane, select the Application tab, and you'll find an option "Allow remote login." With that checked, you can log in from home or anywhere else if you know the account and password information.To log in to a remote computer using Terminal, first log in to your local computer by launching the Terminal application. Then start a program that connects to the remote computer. Some typical programs for connecting over a computer network are
telnet
,ssh
(secure shell),rsh
(remote shell), orAdditional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Remote Logins
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterThe computer you log in to may not be the computer you need to use. For instance, you might have a nifty iMac running Mac OS X on your desk but need to do some work on the main computer in another building. Or you might be a professor doing research with a computer at another university. Your Mac can connect to another Unix computer to let you work as if you were sitting at that computer. You can actually connect to another Mac running Mac OS X (if enabled) too, but you can't run Aqua applications, just the command line. This section describes how to connect to another computer from within the Terminal.If you'd like to set up your computer to allow remote logins, your best bet is to choose System Preferences from the Mac OS X Apple menu in the top left corner of your screen. Within the Sharing pane, select the Application tab, and you'll find an option "Allow remote login." With that checked, you can log in from home or anywhere else if you know the account and password information.To log in to a remote computer using Terminal, first log in to your local computer by launching the Terminal application. Then start a program that connects to the remote computer. Some typical programs for connecting over a computer network are
telnet
,ssh
(secure shell),rsh
(remote shell), orrlogin
(remote login). All of these are supported and included with Mac OS X. In any case, when you log off the remote computer, the remote login program quits and you get another shell prompt from your local computer.Figure 7-1 shows how remote login programs such astelnet
work. In a local login, you interact directly with the shell program running on your local system. In a remote login, you run a remote-access program on your local system; that program lets you interact with a shell program on the remote system.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Transferring Files
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterYou may need to copy files between computers. For instance, you can put a backup copy of an important file you're editing onto an account at a computer in another building or another city. Dr. Nelson could put a copy of a datafile from her local computer onto a central computer, where her colleagues can access it. Or you might want to download 20 files from an FTP server, but not want to go through the tedious process of clicking on them one by one in a web browser window. If you need to do this sort of thing often, your system administrator may be able to set up a networked filesystem connection; then you'll be able to use local programs such as
cp
andmv
. But Unix systems also have command-line tools for transferring files between computers. These often work more quickly than graphical tools. We explore them later in this section.Mac OS X includes bothscp
(secure copy) andrcp
(remote copy) programs for copying files between two computers. In general, you must have accounts on both computers to use these. The syntax ofscp
andrcp
are similar tocp
, but also let you add the remote hostname to the start of a file or directory pathname. The syntax of each argument is:hostname:pathname
hostname: is needed only for remote files. You can copy from a remote computer to the local computer, from the local computer to a remote computer, or between two remote computers.Thescp
program is much more secure thanrcp
, so we suggest usingscp
to transfer private files over insecure networks such as the Internet. For privacy,scp
encrypts the file and your passphrase.For example, let's copy the files report.may and report.june from your home directory on the computer named giraffe.intuitive.com and put the copies into your working directory (.
) on the machine you're presently logged in to. If you haven't set up the SSH agent that lets you useAdditional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Chapter 8: Unix-Based Internet Tools
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterIf you're going to be interacting with the Internet extensively, odds are good that you'll opt for attractive and easy-to-use Aqua applications. Bear with us, though; there's a lot of power in the Unix command-line alternatives, and they're well worth learning.There are a number of excellent web browsers available within Aqua, including Microsoft Internet Explorer, Mozilla, Omniweb, and Opera. While attractive, graphically based web browsers can be slow—especially with flashy, graphics-laden web pages on a slow network.The Lynx web browser (originally from the University of Kansas, and available on many Unix systems) is different because it's a text-based web browser that works within the Terminal application. Being text-only causes it to have some tradeoffs you should know about. Lynx indicates where graphics occur in a page layout; you won't see the graphics, but the bits of text that Lynx uses in their place can clutter the screen. Still, because it doesn't have to download or display those graphics, Lynx is fast, especially over a dialup modem or busy network connection. Sites with complex multicolumn layouts can be hard to follow with Lynx; a good rule is to page through the screens, looking for the link you want and ignore the rest. Forms and drop-down lists are a challenge at first, but Lynx always gives you helpful hints for forms and lists, as well as other web page elements, in the third line from the bottom of the screen. With those warts (and others), though, once you get a feel for Lynx you may find yourself choosing to use it—even on a graphical system.Most importantly, Lynx isn't included with the default Mac OS X distribution, even on the Developer CD-ROM. You'll need to go to the "Get Mac OS X Software..." link off the Apple menu, which opens a web browser and takes you to the Apple web site. From there, find and click on the "Unix Apps & Utilities" link, which then offers a list of useful Unix applications. For now, just download Lynx and run the installer.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Lynx, a Text-Based Web Browser
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterThere are a number of excellent web browsers available within Aqua, including Microsoft Internet Explorer, Mozilla, Omniweb, and Opera. While attractive, graphically based web browsers can be slow—especially with flashy, graphics-laden web pages on a slow network.The Lynx web browser (originally from the University of Kansas, and available on many Unix systems) is different because it's a text-based web browser that works within the Terminal application. Being text-only causes it to have some tradeoffs you should know about. Lynx indicates where graphics occur in a page layout; you won't see the graphics, but the bits of text that Lynx uses in their place can clutter the screen. Still, because it doesn't have to download or display those graphics, Lynx is fast, especially over a dialup modem or busy network connection. Sites with complex multicolumn layouts can be hard to follow with Lynx; a good rule is to page through the screens, looking for the link you want and ignore the rest. Forms and drop-down lists are a challenge at first, but Lynx always gives you helpful hints for forms and lists, as well as other web page elements, in the third line from the bottom of the screen. With those warts (and others), though, once you get a feel for Lynx you may find yourself choosing to use it—even on a graphical system.Most importantly, Lynx isn't included with the default Mac OS X distribution, even on the Developer CD-ROM. You'll need to go to the "Get Mac OS X Software..." link off the Apple menu, which opens a web browser and takes you to the Apple web site. From there, find and click on the "Unix Apps & Utilities" link, which then offers a list of useful Unix applications. For now, just download Lynx and run the installer.With Lynx installed, let's now take a quick tour.The Lynx command line syntax is:
lynx "location"
For example, to visit the O'Reilly home page, enterlynx
"https://www.oreilly.com"
or simplyAdditional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Electronic Mail
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterYou may see a notice that says "You have mail" when you first log in to your system, or later, before a shell prompt. Someone has sent you a message or document by electronic mail (email). With email, you can compose a message at your terminal and send it to another user or list of users. You also can read any messages that others may have sent to you.There are a lot of email programs for Unix. If you use email often, we recommend that you start with whatever program other people in your group use.We start with a brief section on addressing email. Next, you'll see how to send mail from a shell prompt with Berkeley
mail
. Then we introduce sending and reading mail with Pine, a popular menu-driven program that works without a window system. If you'd like to try a graphical program (which we won't discuss here), there are four standout choices: Mail, included with Mac OS X and written by Apple Computer; Entourage, a part of the Microsoft Office X suite; PowerMail, a fast and efficient option; and Eudora, another popular alternative. All programs' basic principles are the same though, and they all can send and receive messages from each other.Some versions of Mac OS X are misconfigured for command-line email, and if your system behaves similar to this:% mail -s testing taylor@intuitive.com Just a test message. . EOT % /etc/mail/sendmail.cf: line 81: fileclass: cannot open /etc/mail/local-host-names: Group writable directory
You need to flip to Appendix A to learn how to make the few changes necessary to fix things. Otherwise, you're ready to continue.Most addresses have this syntax:username@hostnameusername is the person's username, such as jerry, and hostname is either the name of his computer or a central domain name for his entire organization, such asAdditional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Usenet News
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterUsenet, also called "Net News," has thousands of worldwide discussion groups. Each discussion is carried on as a series of messages in its own newsgroup . A newsgroup is named for the kind of discussion that happens there. Each message is a lot like an email message. But, instead of being sent to a list of email addresses, a newsgroup message is sent to all the computers that subscribe to that particular newsgroup—and any user with access to that computer can read and reply to the message.Because Usenet is a public forum, you'll find a variety of people with a variety of opinions—some impolite, rude, or worse. Although most users are friendly and helpful, a few people seem to cause most of the problems. Until you're accustomed to Usenet, be aware that you may be offended by some contributors and attacked ("flamed") by others.To read Usenet groups, you'll need a newsreader program , also called a news client. Many email programs can read news, too. You can use any newsreader; the principles of all are about the same. Some of the more popular Unix newsreaders are
slrn
,nn
, andtrn
. We show how to read news with Pine Version 4.33. If you haven't used Pine before, please read Section 8.2.4 earlier in this chapter.If your system's copy of Pine has been set up to read Usenet messages, when you choose theL
key ("folder list") from the main menu, you'll get a Collection List screen, as shown in Figure 8-7. A collection is a group of folders. A collection can be email folders from your local computer, email folders from other computers, or Usenet newsgroup folders. Figure 8-7 shows two collections: Mail and news on news/nntp. The news collection is selected (highlighted).Figure 8-7: Pine Collection List screenIf your copy of Pine is recent enough to read Usenet, but doesn't seem to do it, check the configuration settings, as described in Section 8.2.6 earlier in this chapter. TheAdditional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Interactive Chat
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterNeed a quick answer from another user without sending an email message and waiting for his reply? Want to have a conversation with your Internet-connected friend in Chile but don't have money for an international phone call? An interactive chat program lets you type text to another user and see her reply moments later. Chatting, or "instant messaging," has become popular. Widely known chat programs are available for Unix; as of this writing, those include Jabber and AOL Instant Messenger. Other programs have been available on Unix systems for years and are included with Mac OS X. We look at two of these:
talk
and IRC.Thetalk
program is simple to use. Give the username (and, optionally, the hostname) of the person with whom you want to chat. Thentalk
will try to notify that person as well as show how to usetalk
to complete the connection with you. Both of your terminal windows will be split into two sections, one for the text you type and the other for the text you get from the other person. You can type messages back and forth until one of you uses Control-C to break the session.One advantage oftalk
is its simplicity; if each of you has a terminal window open, either of you can run the program at any time; if the other person is logged in, he is notified that you want to chat and told how to complete the connection. If both people want to usetalk
on the same computer—even if one of them is logged in remotely (see Section 7.1)—it should work well. Unfortunately, there are severaltalk
versions that don't work with each other. So, the first time you try to chat with someone on another host, which might have anothertalk
version (or other problems), it can take planning. Use an email message or phone call to alert them that you'll trytalk
ing soon, then experiment to be sure that both of you have compatibletalk
systems. After that, you're all set.Here's the syntax:talk username@hostname
If the other user is logged onto the same computer as you, omit theAdditional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Chapter 9: Multitasking
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterUnix can do many jobs at once, dividing the processor's time between the tasks so quickly that it looks as if everything is running at the same time. This is called multitasking.With a window system, you can have many applications running at the same time, with many windows open. But Mac OS X, like most Unix systems, also lets you run more than one program inside the same terminal. This is called job control. It gives some of the benefits of window systems to users who don't have windows. But, even if you're using a window system, you may want to use job control to do several things inside the same terminal window. For instance, you may prefer to do most of your work from one terminal window, instead of covering your desktop with multiple windows.Why else would you want job control? Suppose you're running a program that will take a long time to process. On a single-task operating system such as MS-DOS, you would enter the command and wait for the system prompt to return, telling you that you could enter a new command. In Unix, however, you can enter new commands in the "foreground" while one or more programs are still running in the "background."When you enter a command as a background process, the shell prompt reappears immediately so that you can enter a new command. The original program will still run in the background, but you can use the system to do other things during that time. Depending on your system and your shell, you may even be able to log off and let the background process run to completion.Running a program as a background process is most often done to free a terminal when you know the program will take a long time to run. It's also used whenever you want to launch a new window program from an existing terminal window—so that you can keep working in the existing terminal, as well as in the new window.To run a program in the background, add the
&
character at the end of the command line before you press the Return key. The shell then assigns and displays a process ID number for the program:Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Running a Command in the Background
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterRunning a program as a background process is most often done to free a terminal when you know the program will take a long time to run. It's also used whenever you want to launch a new window program from an existing terminal window—so that you can keep working in the existing terminal, as well as in the new window.To run a program in the background, add the
&
character at the end of the command line before you press the Return key. The shell then assigns and displays a process ID number for the program:% sort bigfile > bigfile.sort & [1] 29890 %
(Sorting is a good example because it can take a while to sort huge files, so users often do it in the background.)The process ID (PID) for this program is 29890. The PID is useful when you want to check the status of a background process, or if you need to cancel it. You don't need to remember the PID, because there are Unix commands (explained in the next section) to check on the processes you have running. Some shells write a status line to your screen when the background process finishes.Here's another example. Mac OS X has a command calledopen
that lets you launch Aqua applications from the Unix command line. You can also feed specific files or directories to theopen
program, in which case it will launch the appropriate Aqua application that can view or display the file (similarly to double-clicking on the file icon in the Finder). For example, to view the /Library directory in the Finder, you can useopen /Library
, but since you want to have it immediately move into the background you need to append the&
suffix:% open /Library & [1] 505
A new Finder window will open up, showing the contents of /Library.In the C shell, you can put an entire sequence of commands separated by semicolons (;
) into the background by putting an ampersand at the end of the entire command line. In other shells, enclose the command sequence in parentheses before adding the ampersand. For instance, you might want to sort a file, then print it afterAdditional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Checking on a Process
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterIf a background process takes too long, or you change your mind and want to stop a process, you can check the status of the process and even cancel it.When you enter the command
ps
, you can see how long a process has been running, the process ID of the background process, and the terminal from which it was run. Thetty
program shows the name of the Terminal where it's running; this is especially helpful when you're using a window system or you're logged into multiple terminals, as the following code shows:% ps PID TT STAT TIME COMMAND 310 std S 0:00.37 -tcsh (tcsh) 510 std R+ 0:00.00 ps 459 p2 S+ 0:00.25 -tcsh (tcsh) % tty /dev/ttyp1
In its basic form,ps
lists the following:- Process ID (PID)
-
A unique number assigned by Unix to the process.
- Terminal name (TT)
-
The Unix name for the terminal from which the process was started.
- Run Time State (STAT)
-
The current state of each job. "S" is sleeping, "R" is runnable, "T" is stopped, and "I" is idle (sleeping for more than 20-30 seconds). Additionally, state can include "+" to indicate it's part of the foreground group process, "E" indicates the process is exiting, and "W" means it's swapped out.
- Run time (TIME)
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Canceling a Process
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterYou may decide that you shouldn't have put a process in the background or the process is taking too long to execute. You can cancel a background process if you know its process ID.The
kill
command terminates a process. The command's format is:kill PID(s)
kill
terminates the designated process IDs (shown under the PID heading in theps
listing). If you do not know the process ID, do aps
first to display the status of your processes.In the following example, thesleep
n command simply causes a process to "go to sleep" for n seconds. We enter two commands,sleep
andwho
, on the same line, as a background process.% (sleep 60; who)& [1] 543 % ps PID TT STAT TIME COMMAND 310 std S 0:00.52 -tcsh (tcsh) 543 std S 0:00.00 -tcsh (tcsh) 544 std S 0:00.01 sleep 60 545 std R+ 0:00.00 ps 459 p2 S+ 0:00.25 -tcsh (tcsh) % kill 544 # Terminated taylor console Feb 6 08:02 taylor ttyp1 Feb 6 08:30 taylor ttyp2 Feb 6 08:32 [1] Done ( sleep 60; who )
We decided that 60 seconds was too long to wait for the output ofwho
. Theps
listing showed thatsleep
had the process ID number 544, so we use this PID to kill thesleep
process. You should see a message like "terminated" or "killed"; if you don't, use anotherps
command to be sure the process has been killed.Thewho
program is executed immediately, as it is no longer waiting onsleep
; it lists the users logged into the system.- The process didn't die when I told it to.
-
Some processes can be hard to kill. If a normal kill of these processes is not working, enter
kill
-9
PID. This is a sure kill and can destroy almost anything, including the shell that is interpreting it.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Chapter 10: Where to Go from Here
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterNow that you're almost to the end of this guide, let's look at some ways to continue learning about Unix. Documentation is an obvious choice, but it isn't always in obvious places. You can save time by taking advantage of other shell features—aliases, functions, and scripts—that let you shorten a repetitive job and "let the computer do the dirty work."We'll close by seeing how you can use Unix commands on non-Unix systems.You might want to know the options to the programs we've introduced and get more information about them and the many other Unix programs. You're now ready to consult your system's documentation and other resources.Different versions of Unix have adapted Unix documentation in different ways. Almost all Unix systems have documentation derived from a manual originally called the Unix Programmer's Manual . The manual has numbered sections; each section is a collection of manual pages, often called manpages; each program has its own manpage. Section 1 has manpages for general Unix programs such as
who
andls
.Mac OS X has individual manual pages stored on the computer; users can read them online. If your system has online manpages, and you want to know the correct syntax for entering a command or the particular features of a program, enter the commandman
and the name of the command. The syntax is:man command
For example, if you want to find information about the programmail
, which allows you to send messages to other users, enter:% man mail . . %
The output ofman
is filtered through a pager in Mac OS X like less automatically. If it isn't, just pipe the output ofman
toless
(ormore
orpg
).After you enter the command, the screen fills with text. Press the spacebar or Return to read more, andq
to quit.Mac OS X also includes a command calledAdditional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Documentation
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterYou might want to know the options to the programs we've introduced and get more information about them and the many other Unix programs. You're now ready to consult your system's documentation and other resources.Different versions of Unix have adapted Unix documentation in different ways. Almost all Unix systems have documentation derived from a manual originally called the Unix Programmer's Manual . The manual has numbered sections; each section is a collection of manual pages, often called manpages; each program has its own manpage. Section 1 has manpages for general Unix programs such as
who
andls
.Mac OS X has individual manual pages stored on the computer; users can read them online. If your system has online manpages, and you want to know the correct syntax for entering a command or the particular features of a program, enter the commandman
and the name of the command. The syntax is:man command
For example, if you want to find information about the programmail
, which allows you to send messages to other users, enter:% man mail . . %
The output ofman
is filtered through a pager in Mac OS X like less automatically. If it isn't, just pipe the output ofman
toless
(ormore
orpg
).After you enter the command, the screen fills with text. Press the spacebar or Return to read more, andq
to quit.Mac OS X also includes a command calledapropos
orman
-k
to help you locate a command if you have an idea of what it does but are not sure of its correct name. Enterapropos
followed by a descriptive word; you'll get a list of commands that might help. To get this working, however, you need to first build the apropos database. This can be done with the following command (you'll needsudo
enabled for this to work):% sudo /usr/libexec/makewhatis
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Shell Aliases and Functions
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterIf you type command names that are hard for you to remember, or command lines that seem too long, you'll want to learn about shell aliases and shell functions. These shell features let you abbreviate commands, command lines, and long series of commands. In most cases, you can replace them with a single word or a word and a few arguments. For example, one of the long pipelines (see Section 6.2 in Chapter 6) could be replaced by an alias or function named (for instance,
aug
). When you typeaug
at a shell prompt, the shell would list files modified in August, sorted by size.Making an alias or function is almost as simple as typing in the command line or lines that you want to run. References in Section 10.1 earlier in this chapter, have more information. Shell aliases and functions are actually a simple case of shell programming.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Programming
- Content preview·Buy PDF of this chapter|Buy reprint rights for this chapterWe mention earlier that the shell is the system's command interpreter. It reads each command line you enter at your terminal and performs the operation that you call for. Your shell is chosen when your account is set up.The shell is just an ordinary program that can be called by a Unix command. However, it contains some features (such as variables, control structures, and so on) that make it similar to a programming language. You can save a series of shell commands in a file, called a shell script , to accomplish specialized functions.Programming the shell should be attempted only when you are reasonably confident in your ability to use Unix commands. Unix is quite a powerful tool and its capabilities become more apparent when you try your hand at shell programming.Take time to learn the basics. Then, when you're faced with a new task, take time to browse through references to find programs or options that will help you get the job done more easily. Once you've done that, learn how to build shell scripts so that you never have to type a complicated command sequence more than once.You might also want to learn Perl. Like the shell, Perl interprets script files full of commands. But Perl has a steeper learning curve than the shell. Also, because you've already learned a fair amount about the shell and Unix commands by reading this book, you're almost ready to start writing shell scripts now; on the other hand, Perl will take longer to learn. But if you have sophisticated needs, learning Perl is another way to use even more of the power of your Unix system.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Appendix A: Configuring Sendmail
- Content preview·Buy reprint rights for this chapterIn the voodoo land of Unix, nothing is more weird and confusing than the low-level mechanism used for sending mail. Mac OS X Unix includes sendmail, a powerful but incredibly complex application that manages communication between command-line mail user agents (such as Pine) and the Internet itself.Unfortunately, Mac OS X has default settings that make it impossible for you to send mail directly from the command line. If you do, you'll probably see an error similar to:
% mail -s practice taylor@intuitive.com < /dev/null sendmail: cannot open /etc/mail/local-host-names: Group writable directory %
The fix is straightforward, and involves a single tweak!All you need to do is change the permission of the root directory withchmod
(and enter your password when prompted):% sudo chmod 755 / password: %
and sendmail should work fine. If you want to also configure your system to receive mail (that is, run as a mail server), you'll need to make one additional change.To configure your server for receiving email, you need to change the configuration of your system so that it knows you want to run a mail server. Make the following two changes to the file /etc/hostconfig:Change MAILSERVER=-NO- to MAILSERVER=-YES- Change HOSTNAME=-AUTOMATIC- to HOSTNAME=host.your.domain
Here's how my /etc/hostconfig looks after these modifications (we've highlighted the two changed lines in bold to have them stand out a bit more):# cat /etc/hostconfig ## # /etc/hostconfig ## # This file is maintained by the system control panels ## # Network configuration HOSTNAME=dsl-138.dsldesigns.net ROUTER=-AUTOMATIC- # Services AFPSERVER=-NO- APPLETALK=en0 AUTHSERVER=-NO- AUTOMOUNT=-YES- CONFIGSERVER=-NO- IPFORWARDING=-NO- MAILSERVER=-YES- MANAGEMENTSERVER=-NO- NETINFOSERVER=-AUTOMATIC- RPCSERVER=-AUTOMATIC- NETBOOTSERVER=-NO- NISDOMAIN=-NO- TIMESYNC=-YES- QTSSERVER=-NO- SSHSERVER=-NO- WEBSERVER=-NO- APPLETALK_HOSTNAME="Big G4 Computer"
To edit this file, use the commandsudo vi /etc/hostconfig
so that you can write the revised version back to your disk without permission problems cropping up.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing! - Appendix B: Quick Reference
- Content preview·Buy reprint rights for this chapter
Table B: The Filesystem: Rearranging, Getting Around cd
Change working directory to your home directory.cd
pathnameChange working directory to pathname.chmod
perm pathnameSet access permissions for the file or directory at pathname.cp
old newCopy file old to file new.CpMac
old newCopy a Macintosh file with resources to a new file.find
top rulesFind pathnames to all files/directories, searching down the tree from top, using rules.locate
patternFind absolute pathnames of all files/directories that match pattern.ls
List names of files in working directory.ls
pathnameList names of files in directory at pathname.Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Return to Learning Unix for Mac OS X
About O'Reilly | Contact | Jobs | Press Room | How to Advertise | Privacy Policy
© 2007, O'Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.