17.4. Where to Go from Here

 <  Day Day Up  >  

17.3. Nine Useful Unix Utilities

So 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. bc

Mac 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. kill

Mac 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. open

What 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. ps

The 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. shutdown

It'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:

  • Now . You can safely shut down by typing shutdown now . (Actually, only the root user is allowed to use shutdown , so you'd really type sudo shutdown now and then type in your administrator's password when asked.)

  • Later today . Specify a time instead of now . Typing sudo shutdown -h 2330 , for example, shuts down your machine at 11:30 p.m. today ( 2330 is military time notation for 11:30 p.m.).

  • Any time in the next 100 years . To make the machine shut down at 5:00 p.m. on November 5, 2005, for example, you could type sudo shutdown 0411051700 . (That number code is in year [last two digits]:month:date: hour :minute format. So 0504061700 means 2005, April 6, 5:00 p.m.)


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:

  • Using the -r flag instead of -h means "restart instead of just shutting down," as in sudo shutdown -r now .

  • You can use shutdown to knock all connected network users off your machine without actually shutting down. Use the -k flag, like this: sudo shutdown -k now .

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, zip

You 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).

Figure 17-3. The top display remains onscreen, automatically updating itself as you work, until you type q to quit the program. The plain-English program names are in there somewhere.


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. top

When 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.)

POWER USERS' CLINIC
Secrets of Virtual Memory

The top command's table offers a fascinating look at the way Mac OS X manages memory. In the "VM" section, for example, you'll see current statistics for pageins and pageouts ”that is, how many times the virtual-memory system has had to "set down" software code for a moment as it juggles your open programs in actual memory. (These numbers are pointed out in Figure 17-3.)

The pageins and pageouts statistics are composed of two different numbers, like this: 45451(0) pageins, 42946(0) pageouts . The big number tells you how many times your Mac has had to shuffle data in and out of memory since the Mac started up. The number in parentheses indicates how much of this shuffling it's done within the last second .

This is the number to worry about. If it stays above zero for a while, your Mac is gasping for RAM (as the hard drive thrashing sounds and program-switching delays are probably also telling you).

In the listing of individual programs, the last four columns provide details about the memory usage of each listed program. The one you care about is the RPRVT (Resident Private) column, which shows how much memory each program is actually using at the moment. (If you remember the About This Macintosh memory graph of Mac OS 9, then you probably remember the filled portion of the memory bars. That's what this number tells you: how much memory each program is actually using.) This number goes up and down as you work, illustrating the miracle of Mac OS X: Programs don't just grab a chunk of memory and sit with it. They put RAM back in the pot when they don't need it.


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  >  


Mac OS X. The Missing Manual
Mac OS X Snow Leopard: The Missing Manual (Missing Manuals)
ISBN: 0596153287
EAN: 2147483647
Year: 2005
Pages: 506
Authors: David Pogue

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net