oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Learning the Mac OS X Terminal Learning the Terminal in Jaguar, Part 1

by Chris Stone, coauthor of Mac OS X in a Nutshell

Editor's note -- After reading the chapters Chris Stone contributed to Mac OS X: The Missing Manual, and previewing his work in Mac OS X in a Nutshell, I asked him to write a few articles for the Mac DevCenter to help people become more comfortable with the Terminal application in Jaguar (Mac OS X 10.2). These tutorials give you a preview of what Chris has covered in the Missing Manual.

If you've read some of his earlier tutorials, this series will feel vaguely familiar. That's because some of the examples here are the same as before. What's different is that this series is for Jaguar and the previous articles were written for Mac OS X 10 - 10.1. Jaguar changed a lot of things, and these new tutorials will show you how to work effectively in Apple's latest operating system.

Getting Comfortable

Mac OS X's Terminal application -- there it sits in your Utilities folder, foreign and mysterious. You've heard that it's a portal to the new world of the Unix command line, a world where your flurries of mouse clicks can be replaced with a just few keystrokes.

But you've been wary of rushing into this new territory where the keyboard is king, concerned that without enough knowledge you might get lost, stuck, or worse. Or maybe you're an adventurer, just waiting to dive into uncharted waters.

This article is for you. Regardless of why you've previously avoided [yourhost:~] yourname%, I'll show you how to take your first steps with the Terminal application in Jaguar. Then I'll walk you through a tutorial that will accelerate your understanding of the Unix command line.

In Part 1 of this series, you'll learn more about what Terminal does and get an overview of the tutorial procedure. In Part 2 you'll jump into the tutorial itself to learn the fundamental Unix commands necessary to get started with just about any command-line procedure.

Then, in Part 3, you'll finish the rest of the tutorial, as well as learn a few more things you can do with the command line.

The Command-Line Interface

The command-line interface (CLI) displayed in Terminal's windows provides access to the Unix shell, which is really just another way to interact with your Mac. The other method, which you're probably more comfortable with, is the Aqua interface. Aqua enables you to tell the Mac what to do by clicking on icons and menus to launch graphical applications.

The shell, on the other hand, allows you to type text commands to accomplish much of the same work. Typically these typed commands launch tiny, single-purpose Unix applications which do some specific work and then quit. The shell itself is an application that plays the go-between for the commands that you enter and the Unix kernel at the core of Mac OS X. There are in fact several shells available. By default Mac OS X uses a shell called tcsh.

If you're curious about why you would want to use the shell in the first place, see the article Why Use a Command Line Instead of Windows? for more information about the CLI versus the Aqua interface.

The Procedure

To help you learn the Terminal application more quickly, I'm going to introduce you to a Unix utility built right into your Mac OS X system. Working with this utility will help you get more comfortable with core Unix commands.

Related Reading

Mac OS X in a Nutshell
A Desktop Quick Reference
By Jason McIntosh, Chuck Toporek, Chris Stone

Installed with Mac OS X is a mechanism that performs important fine-tuning of your system. It's called cron. By using this Unix task-scheduling utility, your system can regularly purge itself of outdated, space-hogging log files, update system databases so utilities like locate can work effectively, and do several other maintenance tasks that keep your system running lean and mean.

The cron utility fully automates this process, meaning that once everything is configured, the housecleaning will happen unattended as scheduled. The good news is that Apple has done the configuration for you. The not-so-good news is that they've scheduled these groups of tasks, or cron jobs, to run between 4:00 and 5:00 in the morning -- a time when your Mac is likely not even on! And if your Mac is never on during these times, these important tasks will never happen. If your Mac is powered on but in deep sleep, the jobs still won't run.

In this tutorial, I'll show you first how to modify the cron schedule, which is read from a file called the system crontab, so that these tasks occur at more reasonable times. I'll then explain how to configure Mac OS X's built-in mail server so that you'll receive report by email every time the cron jobs run.

The Tutorial

For this tutorial, make sure you're running Mac OS X 10.2 or newer (for older versions of OS X, refer to earlier tutorials in this series), and that you're logged in with an administrator's, though not the root, account.

Open the Terminal program, which you'll find in the Applications --> Utilities folder. Once launched, Terminal opens a single window displaying the time of the last login, a greeting, and a third line of text that comprises the prompt. With that window active, anything you type will enter just before the rectangular cursor that follows the prompt. After you type a command, simply press Return or Enter to run it.

The prompt shows the name of your computer (or rather its host name, which can vary), and then identifies your current working directory ("directory" is just the Unix term for "folder"). The current working directory is "where you are," that is, the location in your filesystem hierarchy that your next command will act on. Your initial working directory is always your home directory, which is identified in the prompt by the home directory shortcut character "~".

Screen shot.

To fully display the path to your working directory, use the pwd command: Type pwd (which means "print working directory," though it only displays it) and press Return:

[haru:~] chris% pwd 
[haru:~] chris%

Comment on this articleChris is standing by if you want to chat about using the Terminal app in Jaguar.
Post your comments

As you can see, pwd does its job by displaying the full path, or pathname, to your home directory and providing you with a new prompt when done. This path name begins with the slash character, which represents the root or top-most directory of your filesystem. Note that directories that reside on your system disk do not include that disk's name in their pathnames.

To act on a different set of files, you simply change your working directory using the cd command. Our firs step is to modify the crontab file, which exists in the /etc directory (normally invisible to the Finder). Enter cd followed by a space and the path name of the target directory, /private/etc:

[haru:/etc] chris% cd /private/etc 
[haru:/private/etc] chris% ls

Notice the change in the prompt reflecting the new working directory. If you're curious about what your working directory contains, use the ls, or list command:

[haru:/private/etc] chris% ls
[haru:/etc] chris% ls
6to4.conf                 iftab                     rc.netboot
afpovertcp.cfg            inetd.conf                resolv.conf
appletalk.cfg             kcpassword                resolver
appletalk.nvram.en0       kern_loader.conf          rmtab
atalk                     localtime                 rpc
authorization             magic                     rtadvd.conf
bashrc                    mail                      services
crontab                   mail.rc                   shells

As you can see, there are a lot of items -- quite a bit more than what's shown here -- in /private/etc, including crontab.

The Crontab File

The cron application launches automatically at system startup and runs continuously in the background executing commands as instructed by the crontab files. These files tell cron exactly what commands to run and when to run them. In fact, each user account can have its own crontab file. The system crontab found in /private/etc belongs to the super-user, or root account, and therefore can specify commands requiring the same total system access allowed to root.

Pages: 1, 2

Next Pagearrow