< Day Day Up > |
17.3. Nine Useful Unix UtilitiesSo far, you've read about only a handful of the hundreds of Unix programs that are built into Mac OS X and ready to run. Yes, ls and sudo are very useful tools, but they're only the beginning. Here's a rundown of some more cool (and very safe) programs that await your experimentation. Tip: If you don't return to the $ prompt after using one of these commands, type q or, in some cases, quit , and then hit Enter. 17.3.1.17.3.1.1. bcMac OS 9, Mac OS X, and Windows aren't the only operating systems that come with a basic calculator accessory ; Unix is well equipped in this regard, too. When you type bc and hit Enter, you get a copyright notice and then nothing. Just type the equation you want to solve, such as 2+2 , or 95+97+456+2-65 , or (2*3)+165-95*(2.5*2.5) , and then press Enter. On the next line, bc instantly displays the result of your calculation. (In computer land, * designates multiplication and / represents division. Note, too, that bc solves equations correctly; it calculates multiplication and division before addition and subtraction, and inner parentheses before the outer ones. For more bc tricks and tips , type man bc at the prompt.) 17.3.1.2. killMac OS X offers no shortage of ways to cut the cord on a program that seems to be locked up. You can force quit it (Section 5.1.1), use Activity Monitor (Section 10.26.1), or use kill . The kill program in Terminal simply force quits a program, as though by remote control. (It even works when you telnet or SSH into your Mac from a remote location, as described in Chapter 22.) All you have to do is follow the kill command with the ID number of the program you want to terminate. And how do you know its ID number? You start by running top ”described in a mo-ment ”whose first column shows the PID (process ID) of every running program. Tip: Unless you also use sudo , you can kill only programs that you "own" ”those that are running under your account. (The operating system itself ” root ”is always running programs of its own, and it's technically possible that other people, dialing in from the road, are running programs of their own even while you're using the Mac!) When you hear Unix fans talk about kill online, they often indicate a number flag after the command, like this: kill -9 . This flag is a "non-catchable, non-ignorable kill." In other words, it's an industrial-strength assassin that accepts no pleas for mercy from the program you're killing. If you check top and find out that BeeKeeper Pro's process ID is 753, you'd abort it by typing kill -9 753 and then pressing Enter. 17.3.1.3. openWhat operating system would be complete without a way to launch programs? In Mac OS X's version of Unix, the command is easy enough: open -a , as in open -a Chess . The -a flag allows you to specify an application by name, regardless of where it is on your hard drive. You can even specify which document you want to open into that program by adding a space and then the document's name , like this: open -a Preview MyGraphic.tif . Tip: The -e flag opens any text document in TextEdit (or whatever your default text editor may be), like this: open -e Diary.txt . This shortcut saves you from having to specify TextEdit itself. The real utility of this command might not be apparent at first, but imagine doing something like this in the Finder: Select from a folder of hundreds of HTML files those that contain the word "Sequoia" in their file names and preview them all with the OmniWeb browser, regardless of what application they're actually associated with. You could do it with the help of the Spotlight command, but that would take quite a few steps. In Terminal, though, you just switch to that directory (using the cd command) and type, open -a OmniWeb *Sequoia* . Done! Of course, you may not often find use in simply launching programs and documents this way. Nevertheless you can see how useful open can be when you're writing automated scripts for your Mac, like those used by the cron command scheduler program (Section 17.4.1.2). 17.3.1.4. psThe ps (process status) command is another way to get a quick look at all the programs running on your Mac, even the usually invisible ones, complete with their ID numbers . (For the most helpful results, use the -a, -u, -x , and -w flags like this: ps -auxw . For a complete description of these and other flags, type man ps and hit Enter.) 17.3.1.5. shutdownIt's perfectly easy to shut down your Mac from the a menu. But using shutdown with its -h flag (for halt ) in Terminal has its advantages. For one thing, you can control when the shutdown occurs, using one of these three options:
Tip: Once you set the auto-shutdown robot in motion, you can't stop it easily. You must use the kill command described earlier to terminate the shutdown process itself. To find out shutdown's ID number in order to terminate it, look for the pid number in the output of the shutdown command, or use the top or ps command. There are still more useful flags. For example:
One of the most powerful uses of shutdown is turning off Macs by remote control, either from across the network or across the world via Internet. That is, you can use telnet or SSH (both described in Chapter 22) to issue this command. 17.3.1.6. tar, gzip, zipYou know how Mac OS X 10.4 can create compressed .zip archive files? (If not, check Section 2.8.) Well, as it turns out, you can "stuff" files from within Terminal, too. Actually, you don't encounter StuffIt files much in the Unix world, since they're a Macintosh convention. You're much more likely to run across tar (for combining) and gzip (for compressing) files. And in Windows, people are most familiar with zip files (for combining and compressing). Terminal lets you stuff and combine files in these formats with the greatest of ease. To compress a file, just type gzip , a space, and then the pathname of the file you want to compress (or drag the file directly from the desktop into the Terminal window). When you press Enter or Return, Mac OS X compresses the file. "Tarring" a folder (combining its contents into a single file ”a tarball , as Unix hepcats call it) is only slightly more complicated. You have to specify both the resulting file's name, followed by the actual directory pathname, like this: tar -cf Memos.tar /Users/chris/Memos . Add the -z flag if you want to tar and compress the folder: tar -czf Memos.tar.gz /Users/chris/Memos . To combine and compress files using zip , just specify a name for the zip file and the names of the items to zip, like this: zip StaffordLake.zip Stafford* (which would cram all files in the working directory whose name begins with Stafford into a single archive).
To zip a folder , include the -r flag as well: zip -r Memos /Users/chris/Memos . In any case, if you switch to the Finder, you see that the file or folder you specified is now compressed (with the suffix .gz ), combined (with the suffix .tar ), or both (with the suffix tar.gz or .zip ). Unfortunately, the command line zip utility doesn't handle resource forks properly (Section 16.3.1.2), so stick with tar and gzip if you want to create guaranteed Mac-friendly archives. The best format is a gzipped tarball, which the Finder will properly open with a double-click. (If you only gzip a file without tarring, the Finder won't preserve any resource forks when opening it.) You can also use these utilities to open combined and compressed files, but they can easily overwrite existing items of the same name if you're not careful. Use the Finder or StuffIt Expander to eliminate that worry. Note: The gzip command deletes the original file after gzipping it. The tar and zip commands, on the other hand, "stuff" things but leave the originals alone. 17.3.1.7. topWhen you type top and press Enter, you get a handy table that lists every program that's currently running on your Mac, including the obscure background ones you may not even know exist (Figure 17-3). (To be more specific, you get the top 20 or so. Make the window taller to see the rest.)
You also get statistics that tell you how much memory and speed (CPU power) they're sucking down. In this regard, top is similar to Activity Monitor, described on Section 10.26.1. Tip: If you type top -u , you get a list sorted by CPU usage, meaning the power-hungry programs are listed first. If your Mac ever seems to act sluggish , checking top -u to see what's tying things up is a good instinct. |
< Day Day Up > |