Chapter 1. Why Use Unix?
Why would any sane person want to type in a bunch of funny looking Unix commands when you can just use the mouse? After all, Mac OS X has one—if not the— best looking user interface out there, so what would compel you, a Mac user through and through, to use the Unix command line? That’s a tough sell, but you can boil it down to just one word: Power.
Lying underneath Mac OS X Tiger’s purring Aqua interface is a powerful Unix system, ready to leap into action at a moment’s notice. All you have to do is command Unix to take action. One of the greatest pleasures of using Unix within Mac OS X is that you get the benefit of a truly wonderful graphical environment and the underlying power of the Unix command line. There’s no denying it’s a match made in heaven. Even Apple promotes Mac OS X with the tagline:
|“The simplicity and elegance of the Mac, and the Power of Unix.”|
This chapter sets the stage for the rest of the book, answering the question: “Why use Unix when you have a perfectly good Mac graphical interface?” It’s an important question, and I think that within just a few minutes, you’ll agree that joining the Unix world is really like learning you have a completely separate, and even more powerful, operating system lurking in your machine.
The Power of Unix
Why you should have to remember commands and type them in is an obvious thing to question. If you’re a long-time Macintosh person who is familiar and happy with the capabilities and logic of the Aqua interface, you might need some convincing that Unix is your friend. Here’s why: dipping into the primarily text-based Unix tools on your Mac OS X system gives you more power and control over both your computer and your computing environment. There are other reasons, including:
There are thousands of open source and otherwise freely downloadable Unix-based applications. Can’t afford Adobe Photoshop but still want a powerful graphics editor? The GNU Image Manipulation Program (GIMP) offers a viable solution (see Chapter 10).
Want to search for files by when or who created them? Difficult in the Finder or Spotlight, but it’s a breeze with Unix (see Chapter 5).
And how about managing your files and file archives in an automated fashion? Tricky to set up with the GUI tools, but in Unix, you can set up a cron job to handle this at night while you sleep (see Chapter 5 as well).
Fundamentally, Unix is all about power and control. As an example, consider the difference between using Force Quit from the Apple menu and the Unix programs ps and kill. While Force Quit is more attractive, as shown in Figure 1-1, notice that it lists only graphical applications.
By contrast, the ps (processor status) command used from within the Terminal application (/Applications/Utilities/Terminal) shows a complete and full list of every application, utility, and system process running on your Mac, as shown here:
ps -axPID TT STAT TIME COMMAND 1 ?? S<s 0:00.31 /sbin/launchd 31 ?? Ss 0:00.56 /usr/sbin/syslogd -m 0 -c 5 37 ?? Ss 0:02.30 kextd 41 ?? Ss 0:03.48 /usr/sbin/configd 42 ?? Ss 0:00.47 /usr/sbin/coreaudiod 43 ?? Ss 0:00.76 /usr/sbin/diskarbitrationd ... 375 p2 Ss 0:00.03 login -p -f taylor 376 p2 S+ 0:00.04 -bash 437 p3 Ss 0:00.05 -bash 455 p3 R+ 0:00.01 ps -ax
That’s more than the few applications Force Quit shows you. Of course, the next thing that’s running through your head is “Sure, but what does all that output in the Terminal mean to me, and what do I do with it?” This is the key reason to learn and work with the Unix side of Mac OS X: to really know what your Mac’s doing and be able to make it match what you want and need your Mac to do.
Okay, now let’s go back and look at the output from running the ps -ax command. First off, you’ll see that we added some options (or flags or switches) to the ps command; the options are the -ax bit. The -ax option tells ps to display all of the programs and processes being run by all of the users (including you and the system, itself) on the system. When the Terminal displays the results of the ps -ax command, you’ll see that it adds a “header” to the output:
ps -axPID TT STAT TIME COMMAND 1 ?? S<s 0:00.31 /sbin/launchd
Think of the headers the same way you would when looking at an Excel spreadsheet with a bunch of columns. Each column in that spreadsheet should have a column head to help define what you see underneath. The same applies here. In the very first line of the information returned, you’ll see
COMMAND. These relate to:
The command’s process identification number (or PID for short).
The terminal type the process is running in; if you see two question marks (
??), that means the process is running outside of a Terminal window or display.
Tells you the status of the command.
Tells you the amount of time it took to run that particular process, or how long that process has been running in minutes and seconds. For example, the
0:00.31you see in the output means that it took, roughly, a third of a second for the launchd process to start and run.
Gives you the entire pathname to the process that’s running; for example, /sbin/launchd tells you that the process that’s running is launchd (a daemon, or system service, as noted by the d at the end of its name), which is located in the /sbin directory.
Great! So now you know what all that means, but you still don’t know how this relates to Force Quit, right? Well, be patient, we’re getting there.
Once you know a process’s PID number, you can then issue the Unix kill command to, well, kill that process. For example, let’s say that Microsoft Word decided to lock up on you and you’re stuck with the Spinning Beach Ball of Death (SBBoD) syndrome. After you finish screaming, you need to kill Microsoft Word, but in order to do so, you first need its process number. For this, we’ll add the grep command, which is basically a Unix search tool that you use to search for words or numbers in files, or in this case, the output of a command:
ps -ax | grep Word2847 ?? S 134:05.69 /Applications/Microsoft Office X/Microsoft Word /Application 3665 std R+ 0:00.00 grep Word
This tells us that Microsoft Word’s PID is 2847, as noted by the first set of numbers in the command’s output. Now all you need to do to kill Word is issue the following command:
After typing that and hitting the Return key (an activity known as “entering a command”), Microsoft Word promptly quits, closing all its windows without first saving any of your previous work. But since Word was locked in a deep freeze, you wouldn’t have been able to save your changes anyway, right? And if you had used the Force Quit window, you wouldn’t be able to save changes there, either.
Batch Renames and Extracting File Lists
Here’s another example. Suppose you just received a CD-ROM from a client with hundreds of files all in the main folder. Now let’s say you only need those files that have -nt- or -dt- as part of their filenames, and that you want to copy them from the CD to your Home directory. Within the Finder, you’d be doomed to going through the list manually, a tedious and error-prone process. But on the Unix command line, this becomes a breeze:
cp *-dt-* *-nt-* ~
The first command, cd /Volumes/MyCDROM, takes you to the Volumes directory, which is where the CD (named MyCDROM) is actually mounted on your Mac’s filesystem. The second command, cp *-dt-* *-nt-* ~, breaks down as follows:
This is Unix’s copy command.
- *-dt-* *-nt-*
This tells the cp command to look for any items on the CD that have either -dt- or -nt- in their filenames. Unix recognizes the asterisks (*) as wildcards as part of the search string. By placing an asterisk before and after each item (*-dt-* and *-nt-*), you’re telling Unix to find any file that has either -dt- or -nt- anywhere in its filename.
The tilde character (or squiggle, in Unix-speak) simply refers to a user’s Home folder (or directory).
By placing the tilde (~) at the end of the command line, you’re telling cp to locate any file that has -dt- or -nt- in its filename, and copy those files to your Home directory.
Fast, easy, and doable by any and all Mac OS X users.
There are a million reasons why it’s helpful to know Unix as a Mac OS X power user, and you’ll see them demonstrated time and again throughout this book.
Finding Hidden Files
You might not realize it if you only work in the Finder, but your system has thousands of additional files and directories that you can find much more easily on the command line. These hidden files are known in the Unix world as dot files , because the file or directory has a period (.) as the first letter of its name. For example, in your Home directory you have a file called .bash_profile that contains specific instructions on how you want your Terminal session set up. But when you view your Home folder in the Finder, this file is hidden, as shown in Figure 1-2. Instead, all you see are the default set of folders (Desktop, Documents, Library, Movies, Music, Pictures, Public, and Sites) and a file called myopen.
To view the dot files in the Terminal, type the file listing command (ls), along with its -a option (for list all, which shows me the hidden dot files), and suddenly you realize that there are lots more files in that directory:
ls -a. .bash_history .ssh Mail .. .gimp-1.2 .sversionrc Movies .DS_Store .kde .viminfo Music .ICEauthority .pine-debug1 .xscrabble.save Pictures .Trash .pinerc Desktop Public .Xauthority .profile Documents Sites .angband .qt Library myopen
Thought I don’t always need the power, I like knowing that I can get to, view, and even edit every file on my computer if I need to. All I need to do is launch the Terminal application, type in a few simple commands, and I’m on my way.
Thousands of Free Applications
This should appeal to anyone who is a part of the Macintosh community: by warming up to Unix and its command line, you are joining the much-lauded open source movement, since Mac OS X is based on an open source Unix operating system, called Darwin. What’s excellent is that there are thousands of different applications available for open source operating systems, including design, development, scientific, and business applications that compare quite favorably to expensive commercial alternatives. Also, don’t make the mistake of assuming that all open source applications are command-line applications! Some of the very best applications, like The GIMP graphics editor (http://www.gimp.org) and the NeoOffice/J suite (http://www.neooffice.org) are designed to work within either X11 or directly in Mac OS X’s Aqua GUI environment.
Commands Included with Unix
While this book covers only about 50 of the most basic Unix commands, there are over a thousand Unix commands included with Mac OS X—and you can’t see some of these commands without accessing the command line. From sophisticated software development environments to web browsers, file transfer utilities to encryption and compression utilities, almost everything you can do in the Aqua interface—and more—can be done with a few carefully chosen Unix commands.
If you’re a software developer or just curious about programming, for example, you’ll want to install the optional Xcode Tools , included with Mac OS X Tiger. The Xcode Tools give you a full, professional-grade software development environment that lets you develop new applications in Objective-C, C, or C++. Pretty nice for something included free with your computer, eh?
If you’re eager to install Xcode Tools right now, flip forward to “Xcode” in Chapter 4.
Downloading Unix Software from the Web
While Mac OS X is a capable Unix system from the get-go, there are many, many wonderful Unix-based applications you can download and add to your system, too, and almost all of them are free for the downloading. In addition to command-line tools and utilities, there are hundreds of graphical applications built within the X Window System, a standard Unix graphical interface that Apple includes with your Mac OS X system!
Learn how to install X11 in “Installing X11” in Chapter 9.
Two standout applications in the X11 world are NeoOffice/J, a complete and robust replacement for the Microsoft Office Suite (that’s free!), and The GIMP, an awkwardly named photo and graphics editor that compares quite favorably to Adobe Photoshop. Both are examined in Chapter 9.
Chapter 10 talks about the Fink program, a tool that makes it a breeze for you to find, download, and install these thousands of free applications from the Web, including both OpenOffice and The GIMP. Chapter 10 alone will pay for this book a hundred times over!
Power Internet Connections
If you’re someone who uses the Internet daily, you already know that there are a bunch of useful Mac OS X applications available to help you be efficient. But lots of them seem to have a price tag attached, even a simple FTP program like Fetch (http://www.fetchworks.com). But why spend $25 on an application when you can use Mac OS X Tiger’s built-in ftp command-line utility for free!
For example, if you wanted to download the cover image for this book from O’Reilly’s web site, you could use the following commands (as noted in
ftp ftp.oreilly.comConnected to tornado.east.ora.com. 220 ProFTPD 1.2.10 Server (ftp.oreilly.com) [172.31.173.9] Name (ftp.oreilly.com:taylor):
anonymous331 Anonymous login ok, send your complete email address as your password. Password:
[Type in your email address here]ftp>
get 0596009151.giflocal: 0596009151.gif remote: 0596009151.gif 229 Entering Extended Passive Mode (|||38666|) 150 Opening BINARY mode data connection for 0596009151.gif (217245 bytes) 100% |*************************************| 212 KB 45.72 KB/s 00:00 ETA 226 Transfer complete. 217245 bytes received in 00:04 (44.22 KB/s) ftp>
That downloads the image file for the cover of this book to your Mac, which is nice, but what if you want to look at it? Sure, you could go to the Finder, find the file, and then double-click on the file’s icon to open it in Preview, but that’s a lot of work. However, with a little help from Unix, you can just type in the following command:
open 0596009151.gifMacDave:~ taylor$
The open command, which is special to Mac OS X, examines the file it’s supposed to open (0596009151.gif), detects which application should open it by default (something you can see in a file’s Get Info window), and then opens the file in Preview—all in a fraction of a second! See how much time Unix just saved you (not to mention the $25!)?
From logging in to your Mac from remote locations to transferring files from your system to a server using an encrypted connection, Mac OS X’s Unix command line is quite powerful. But don’t take my word for it. Chapter 8 takes you on a detailed tour of command-line Internet utilities.
A Simple Guided (Unix) Tour
The first step is to launch the Terminal application, through which you’ll interact with the command shell, the program within which you type in your commands and the responses are shown. Terminal is tucked into the Utilities folder in your Applications folder, which you can quickly get at from the Finder with the Shift-⌘-U keyboard shortcut.
Since you’ll be using the Terminal application a lot throughout this book (and hopefully in the future, as you grow more comfortable with Unix), you should drag the Terminal’s icon to the Dock so it’s always at the ready. Or, if the Terminal’s already running, you can Control-click on its application in the Dock and select “Keep in Dock” from its contextual menu, as shown in Figure 1-3.
Throughout the following example, type in the commands you see in
bold, pressing the Return key after each one (again, this is known as “entering a command” in Unix-speak). Preceding each command, I’ve included some comments to let you know what you’re about to do:
Without any arguments, the cd command moves you to your Home directory:
The pwd (or present working directory) command shows you the path for the directory you’re currently in:
Use the ls command to list the files in your Home directory; compare this listing with the picture of the Finder window shown in Figure 1-2. If you omit the -a option, then all the hidden dot files stay hidden in this directory:
lsDesktop Mail Pictures myopen Documents Movies Public Library Music Sites
Now let’s change directories to your Library folder:
Use the ls command again to see what’s inside (there’s very little here you’ll need to mess with):
lsApplication Support FontCollections Preferences Assistants Fonts Printers Audio Icons Recent Servers Autosave Information Indexes Safari Caches Internet Plug-Ins Snapz Pro X Classic Keyboard Layouts Sounds ColorPickers Keychains Syndication Cookies Logs iMovie Documentation Mail iTunes Favorites Metadata
Now let’s go back a directory. For this, use the .. shortcut for moving up one directory in the filesystem. In this case, since you were in your Library folder (/Users/taylor/Library, or just ~/Library), the following command moves you back to your Home directory (as noted by the pwd command that follows):
Finally, when it’s time to quit Terminal, use the exit command rather than just quitting the application with ⌘-Q:
Don’t worry if you aren’t sure exactly what each of those commands does: we’ll explore each one in great detail as the book proceeds.
The 10 Most Common Unix Commands
List files or directories.
original_file(or files) from one location to another.
Move a file or files; the original is deleted once complete.
Remove a file, set of files or folders full of files.
Use the rm command with caution; there’s no “Trash” where things are moved to. Once you’ve used rm to delete something, it’s gone forever.
Display your present working directory; this is where you currently are in the filesystem.
Change to the specified directory in the filesystem. Without any arguments, it’s a shortcut for changing back to your Home directory.
Access Mac OS X’s built-in documentation for the Unix commands. To read the man page for the ls command, for example, type in
Display a long text file, one screen at a time. Pressing the spacebar gets the next page when you’re ready, and pressing Q at any time quits the program and returns you to the command prompt.
Search for the specified pattern across as many files as you desire. A fast way to find that email message you sent to Uncle Linder, for example.
Shows you which applications and processes are running on your system, including those that the Finder’s Force Quit window ordinarily hides.
There’s a whole world of Unix inside your Mac OS X system, and it’s time for you to jump in and learn how to be more productive, more efficient, and gain remarkable power as a Mac user. Ready? Let’s go!