Learning the Mac OS X Terminal: Part 1

Editor's note -- After reading the chapters Chris Stone contributed to Mac OS X: The Missing Manual, I asked him to write a couple of articles for the Mac DevCenter because I believe that understanding the Terminal application adds value to Mac OS X. These tutorials give you a preview of what Chris has covered in the book.

However, none of the first three parts of the series will work in Mac OS X 10.2 (Jaguar) or newer. Jaguar brought several major changes to the OS that require significant changes to the procedure. I'll be posting Jaguar compatible updates to these articles shortly.

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, or stuck, or worse. Or maybe you're an adventurer who is just waiting to dive into uncharted waters.

This article is for you. Regardless of why you've previously avoided [localhost:~] yourname%, I'll show you how to take your first steps with the Terminal application. 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. You’ll then jump into the tutorial itself to learn the fundamental Unix commands you’ll need to know to get started with just about any command-line procedure.

Then, in Part 2, 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 that you're probably more comfortable with is the Aqua interface. Aqua enables you to click on icons and menus, and to launch graphical applications by telling the Mac what to do.

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-duty Unix applications that do specific jobs 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.

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 the core Unix commands.

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, you can have your system 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.

In this tutorial, I'll show you first how to modify the cron schedule, which is read from a file called the 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 and the Mail application so that you’ll receive an emailed report every time the cron jobs run!

The tutorial

For this tutorial, make sure you're running Mac OS X 10.1 or newer, and that you’re logged in with an administrator’s account, 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 a greeting and a second 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 "~".

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:

[localhost:~] chris% pwd
/Users/chris
[localhost:~] chris%

For those of you trying the command line for the first time, how's it going? As for you experienced Unix users, how does the Mac OS X experience compare?

As you can see, pwd does its job by displaying the full path, or path name, 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. We’ll first be modifying 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:

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.