Chapter 1: . . . And UNIX Lurks Beneath AL In This Chapter ✓ Why use UNIX? ✓ Introducing UNIX commands ✓ Creating text files MA ✓ Exploring deep inside Mac OS X TE RI ✓ Doing things with the keyboard A GH TE D s I mention in the first chapter of the book — at the beginning of our Snow Leopard odyssey — UNIX lurks deep beneath the shiny Aqua exterior of Mac OS X. UNIX is a tried-and-true operating system that’s been around for decades, since the days when mainframe computers were king.
638 Why Use the Keyboard? UNIX keyboarding is fast Why on Earth would any red-blooded Macintosh owner want to leave the comfort of the mouse to use a keyboard? After all, the graphical user interface is what made the Macintosh great in the first place. With the Finder, you can navigate and manage the various files on your hard drive with a few clicks. This sounds simple enough, but for some tasks, using the keyboard can be just as fast, if not faster.
Why Use the Keyboard? 639 For example, consider how many times you’d have to click a mouse in the Finder to do the following: 1. Find all files that begin with the letters MyDocument. 2. From this list of files, add a number to the beginning of the filename, indicating its size in kilobytes. 3. Save the names of all altered files to a text file.
640 Why Use the Keyboard? Automate to elevate If all these benefits are beginning to excite you, hold on to your socks! Not only can you perform complex commands with the command line, you can go even one step further: automation. If you find yourself using the same set of commands more than once, you’re a likely candidate for using automation to save time. Instead of typing the list of commands each time, you can save them to a text file and execute the entire file with only one command.
Uncovering the Terminal 641 Uncovering the Terminal The best way to find out how to use command line is to jump right in. Mac OS X comes stocked with an application named Terminal. The Terminal application is where you enter commands in the command line. It’s located in the Utilities folder within the Applications folder on your hard drive — choose Applications➪Utilities. Double-click the icon, as shown in Figure 1-1, to launch Terminal. Figure 1-1: Find the Terminal application in your Utilities folder.
42 Uncovering the Terminal As you might guess, this text details the last time that you logged into the Terminal. The last line, however, is the more important one. It’s called the prompt. The prompt serves some important functions. First, it lists the current directory, which is listed as ~. A tilde character (~) denotes a user’s Home directory. By default, you’re always in your Home folder each time you begin a new session on the Terminal.
Uncovering the Terminal 643 The complementary cd command (lowercase) — which incidentally stands for change directory — opens any folder that you specify. It works much the same as double-clicking a folder in the Finder: The difference is that following the cd command, you don’t immediately see all the folder’s content. However, the cd command requires a parameter (extra options or information that appear after the command) so that your Mac knows which folder to open.
644 UNIX Commands 101 Figure 1-2: Drag a file from a Finder window into Terminal to display its path. You can even use the mouse while entering commands in the Terminal. Click and drag your mouse over text to select it. From there, you can copy to the Clipboard as you might expect with any other application. UNIX Commands 101 To use the command line effectively, familiarize yourself with the commands that are available to you.
UNIX Commands 101 645 Besides flags, UNIX commands sometimes also have operands. An operand is something that is acted upon. For example, instead of just entering the ls command, which lists the current directory, you can add an operand to list a specific directory: ls ~/Documents/myProject/ The tilde (~) denotes the user’s Home directory. Sometimes a command can take multiple operands, as is the case when you copy a file.
646 UNIX Commands 101 In a similar vein, you can get the space character to be accepted by a command by adding an escape character. To escape a character, add a backslash (\) immediately prior to the character in question. To illustrate, try the last command with an escape character instead. Note that this time, no quotation marks are necessary. cd /Desktop\ Folder You can use either quotation marks or escape characters because they’re interchangeable.
Working with Files 647 Leopard uses by default is bash — another common shell is tcsh. Use the autocompletion features of bash to autocomplete both commands and filenames. To demonstrate, begin by typing the following: cd ~/De Then press the Tab key. The result is that the shell predicts that you will want to type cd ~/Desktop/ Of course, if you have another folder that begins with the letters De in the same folder, you might need to type a few additional characters.
648 Working with Files Similarly, a folder named myFolder that resides in the current user’s Documents folder would have a path like this: ~/Documents/myFolder/ As you’ve probably surmised, a folder and a directory are two different names for the same thing. Folder is the name with which most Mac users are familiar, and directory is a term that UNIX power users prefer. I use the terms interchangeably throughout the remainder of the chapter.
Working with Files 649 Furthermore, when you navigate your hard drive by using paths, you can jump directly to your desired destination from any place. When you enter cd .., it is in relation to your current position, whereas entering cd /Users/markchambers will always take you to the same directory, regardless of your starting point.
650 Working with Files A close cousin to the cp (copy) command is the mv (move) command. As you can probably guess, the mv command moves a folder or file from one location to another. (I told you that all this character-based stuff would start to make sense, didn’t I?) To demonstrate, this command moves MyDocument from the Desktop folder to the current user’s Home folder: mv ~/Desktop/MyDocument ~/MyDocument Ah, but here’s the hidden surprise: The mv command also functions as a rename command.
Useful Commands 651 Opening documents and launching applications Launching applications and opening documents is child’s play for a UNIX pro like you. The open command does it all. For example, to bring the Finder to the foreground without touching the mouse, use open /System/Library/CoreServices/Finder.app To open a document from the command line, follow a similar scheme. For example, to view an image named myImage.
652 Useful Commands Append a number to the cal command to display a 12-month calendar for that year. The number that follows the cal command is the year for which you’d like to see a calendar. For example, to view a calendar for 1970, type cal 1970. The result appears in Figure 1-5. Figure 1-5: Type cal followed by a year to view the 12 months of that year. Append a month number and a year number to display the calendar for that month. For example, to view a calendar for April 2010, type cal 04 2010.
Useful Commands 653 in Figure 1-6. These activities are called processes; some are created when you launch applications, and others are simply tasks that Mac OS X has to take care of to keep things running smoothly. Figure 1-6: The top command displays all running processes. Besides listing the names of the various processes currently in use, top tells you how much of your CPU is being devoted to each process. This lets you know what process is currently hogging all your computing power.
654 UNIX Cadillac Commands ps –aux The man page for ps explains what each flag means. (Read more about using the man command in the earlier section “Help is on the way!”) UNIX Cadillac Commands Besides working with files and processes, the command line has all kinds of sophisticated commands. For example, with the command line, you have instant access to a variety of tools for finding files or even stringing together commands.
UNIX Programs That Come in Handy 655 find ~/ -name “Fonts” | less When the results fill up one page, the data stops and waits for you to press any key (except the Q key) to continue. When you reach the end of the results, press Q to quit and return to a command-line prompt. UNIX Programs That Come in Handy As a Macintosh user, you might be surprised to know that many applications on your hard drive don’t reside in one of the typical Applications folders of Mac OS X.
656 UNIX Programs That Come in Handy This is the rough-and-tumble world of UNIX, which preceded the Macintosh by many years. Perhaps this will also help you to appreciate why the Macintosh was so revolutionary when it was introduced. (You can just hear the designers crowing, “We’ll call this a menu! Yeah, that’s the ticket!” The only graphics that you’d see on your monitor were the comics and sticky notes that you stuck to the bottom.) At the bottom of the screen is a menu of common commands.
UNIX Programs That Come in Handy 657 provided a command that can handle them all: curl. The curl command is competent at all the standard network protocols. To see it in action, pass a Web address (or URL, to The Enlightened) to the curl command: curl http://www.mlcbooks.com The result is that you see the HyperText Markup Language (HTML) page that’s located at www.mlcbooks.com. Because this isn’t particularly useful for most people (it’s not very easy to read), you need to add the letter o as a flag.
658 UNIX Programs That Come in Handy This example uploads the README.curl file from the Desktop folder that I downloaded earlier to an imaginary FTP server. How do you spell success? C-u-r-l! Sure, HTTP and FTP are handy, but did you know that there are many other protocols for network communications? One of the niftier ones is the Dictionary protocol. With it, you can look up words from any server that understands the protocol. Suppose, for example, that you want to know the meaning of the term DVD.