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 Lion 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.
674 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? 675 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.
676 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 677 Uncovering the Terminal The best way to find out how to use the command line is to jump right in. Mac OS X comes stocked with an application named Terminal. As I mentioned earlier, the Terminal application is where you enter commands in the command line, and it’s located in the Utilities folder within the Applications folder on your hard drive — choose Applications➪Utilities (from the keyboard, press Shift+Ô+U). Double-click the icon, as shown in Figure 1-1, to launch Terminal.
78 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 ~ in the above example. 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 679 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.
680 UNIX Commands 101 Drag-and-drop is also at your disposal. After you play around with the Terminal for a while, you’ll find yourself bored to tears typing the long paths that represent the files on your hard drive. To automatically enter the path of a file or folder to a command, simply drag it to the active Terminal window, as shown in Figure 1-2. The file’s full path instantly appears at the location of your cursor. (Thanks, Apple!) You can even use the mouse while entering commands in the Terminal.
UNIX Commands 101 681 Anatomy of a UNIX command UNIX commands can perform many amazing feats. Despite their vast abilities, all commands follow a similar structure: command The simplest form of a UNIX command is the command itself. (For a basic discussion on UNIX commands such as ls, see the earlier section, “A few commands to get started.
682 UNIX Commands 101 The cd command stands for change directory. cd /Desktop Folder The result is an error message: -bash: cd: /Desktop: No such file or directory The problem is that a space character isn’t allowed in a path. To get around this problem, simply enclose the path in double quotation marks, like this: cd “/Desktop Folder” Mac OS X lets you use either double or single quotation marks to enclose a path with spaces in it.
Working with Files 683 Figure 1-3: Use the man command to display help information. 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. This gives the autocompletion feature more information to help it decide which characters you want to type.
684 Working with Files Paths Before you dive into UNIX commands, you should first know a few facts . . . nasty things, facts, but you can’t earn your pair of techno-wizard suspenders without ’em. For starters, as a Mac user, you might not be familiar with how paths work in UNIX. A path is simply a textual representation of a folder or file.
Working with Files 685 Here’s a faster way.
686 Working with Files password. If you don’t know what the password is, you probably shouldn’t be using sudo. Your computer’s administrator should have given you an appropriate password to use. After you enter the correct password, the command executes as desired. In case you’re curious, sudo stands for set user and do. It sets the user to the one that you specify and performs the command that follows the username.
Useful Commands 687 Once again, deleting files and folders requires that you have permission to do so. In other words, any time that you manipulate files with the command line, you’re required to have the proper permission. If your current user lacks these permissions, using sudo helps. You should also check to make sure that your target is correctly spelled and that no pesky spaces that could wreak carnage are lurking in the command.
688 Useful Commands Figure 1-4: Type cal to view a calendar for the current month. 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. Use the –m flag to specify the current year, as in cal –m 08 for August of this year. Another useful command that’s related to the cal command is date.
Useful Commands 689 Processes Have you ever been curious as to why your hard drive seems to spin and grind on occasion while your system is seemingly inactive? Mac OS X sometimes has a lot of stuff going on behind the scenes. To discover just what your computer is busy doing at any time, use the top command to display all the actions that your computer is currently performing, as shown in Figure 1-6.
690 UNIX Cadillac Commands Like top, another handy command for examining process info is ps (short for process status). Most often, you’ll want to append a few flags to the ps command to get the information that you desire. For example, try the following command: ps –aux The man page for ps explains what each flag means.
UNIX Programs That Come in Handy 691 to another one that follows — for example, many UNIX commands produce large amounts of information that can’t all fit on one page. (You might have noticed this behavior when you used the locate command.) Joining two commands or functions together with the pipe command is piping. To tame the screens full of text, pipe the find function to the less command. The less command provides data one page at a time.
692 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 693 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. This specifies where you would like to save this file upon download.
694 UNIX Programs That Come in Handy 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. Enter the following command to find out: curl dict://dict.