Chapter 1. The Macintosh Terminal
Welcome to the Macintoshs best-kept secret: the Terminal! If youve ever browsed the Utilities folder, youve probably seen this icon:
Maybe youve even launched the Terminal and seen a plain, dull-looking window appear, displaying mysterious words:
But if youre like most users, this is probably as far as youve explored. And that is a shame, because the Terminal is one of the most powerful programs for controlling your Mac.
What is the Terminal? What does it do? And why should you care? Lets answer the last question by telling a few stories:
Youre running Microsoft Word for the Mac when its window suddenly freezes. You type, but nothing happens. You try to quit Word, but it doesnt respond. In desperation, you go to the application dock, select the Word icon, and choose Force Quit. Even this has no effect! You are stuck and have no choice but to reboot your Mac.
You have a folder of 1,000 PDF files named file1, file2, file3, and so on. For compatibility with a coworkers computer, you need to rename these files to have .pdf extensions. The Finder doesnt seem to have any way to perform these renames in bulk, so you do them one file at a time (click, click, click) until your hands cramp.
Last week, you copied a huge folder of files (and all its subfolders, 10 levels deep) from your Mac to a server on your network. The transfer took over an hour. During the next few days, you modified a few dozen of the original files, and now you want to copy the changed files to the remote server. Of course, you dont want to copy the entire folder again and wait a whole hour! You want to copy just the files that have changed. Unfortunately, you didnt keep track of which ones you modified, so you hunt them down and copy them one by onewhich ends up taking even longer than an hour.
Do these stories sound familiar? In each case, there seems to be no simple solution using the Mac Finder, and you wind up wasting time: rebooting, clicking icons one by one, or hunting through large folders by hand. Well, we have good news. These problems are all easily solved by typing and running commands in the Terminal. In fact, here are the commands that solve our three problems:
killall -KILL 'Microsoft Word'
Terminate Word for i in file*; do mv $i $i.pdf; done
Rename your PDFs rsync -aE myfolder server:
Copy changed filesThese short, somewhat cryptic commands get the job done quickly. The Terminal can save you minutes, hours, or even days of work if you learn the right commands. Thats what this book is all about.
By the way, if youre a system administrator of multiple OS X computers, youre going to love the Terminal. Its command line is outstanding for automating system tasks.
Whats in This Book?
This book is a short guide to the Terminal, not a comprehensive reference . We cover important, useful aspects of the Terminal (and its partner, the shell) so you can work productively. We do not, however, present every single command and every last option (our apologies if your favorite was omitted), nor delve into detail about OS X internals. Short, sweet, and essential, thats our motto.
We focus on commands, the words typed on a command line to tell your Macintosh what to do. Heres an example command that counts lines of text in a file, myfile:
wc -l myfile
Well cover the most important commands for the average user, such as ls
(list files), grep
(search for text in a file), kill
(terminate programs), and df
(measure free disk space), plus some advanced commands like dscl
(manage users and groups) and launchctl
(run services and scheduled jobs). We assume you are already familiar with the Mac desktop and the Finder.
Weve organized the material by function to provide a concise learning path. For example, to help you view the contents of a file, we introduce all file-viewing commands together: cat
for short text files, less
for longer ones, od
for binary files, and so on. Then we explain each command in turn, briefly presenting its common uses and options.
At press time, the current version of OS X is Lion (10.7).
Whats the Terminal?
The Terminal is an application that runs commands. If youre familiar with DOS command lines on Microsoft Windows, the Terminal is somewhat similar (but much more powerful).
Inside each Terminal window, there is a special program running called a shell. The shell does four simple things:
It displays a prompt in the Terminal window, waiting for you to type a command and press Enter.
It reads your command and interprets any special symbols you typed.
It runs the command , automatically locating any necessary programs.
It prints the output , if any, in the Terminal window.
The Terminals job is merely to open windows and manage shells. Using the Terminal, you can resize the windows, change their colors and fonts, and perform copy and paste operations. But its the shell that is doing the real work of reading and running commands. shows how the Terminal and the shell work together: when you peer into a Terminal window, you are viewing a shell, which in turn interacts with your Macintosh.
Figure 1-1. Viewing OS X through the Terminal and the shell
Whats a Command?
OS X comes with over 1,000 commands for file manipulation, text editing, printing, mathematics, computer programming, typesetting, networkingyou name it. A typical command is run in a shell by typing its program name, followed by options and arguments, like this:
wc -l myfile
The program name (wc
, the word count program) refers to a program somewhere on your Mac that the shell will locate and run. Options, which usually begin with a dash, affect the behavior of the program. In the preceding command, the -l
option tells wc
to count lines and not words. The argument myfile
specifies the file that wc
should read and process.
Case Sensitivity
The commands in this book should be entered exactly, using the same capital (uppercase) and small (lowercase) letters we provide.In other words, commands are case-sensitive. If a command is wc -l
(small L) but you type wc -L
(capital L), it will not work.
In some situations, capital and small letters are equivalent. Specifically, the names of files and folders are case-insensitive, so when they appear on a command line, you can use capital or small letters as you see fit. Nevertheless, the rest of the command line is case-sensitive, so we recommend not changing the case of any letters in the presented commands.