BOLTS : Click to return to MacEdition homepage

Yet another Mac OS X command line introduction (Part 2)

Back to part 1

By Mark Dalrymple, August 5, 2002

Command history

Feedback Farm

Have something to say about this article? Let us know below and your post might be the Post of the Month!

Your shell remembers the last several dozen commands that you’ve run. You can use this stored history to reduce your typing burden if you’re running a command similar to what you’ve run before.

The history command will show the stored history. For example, here are the last dozen lines run in the session I’m currently using:

% history | tail -12
    128  12:16   du -sk Pictures/
    129  12:17   pwd
    130  12:17   du -sk .
    131  12:18   du -sk Library Pictures
    132  12:23   du -sk *
    133  12:31   man -k filter
    134  12:33   du -sk * | sort -n
    135  12:38   man sort
    136  12:39   du -sk * | sort -rn
    137  12:40   ls -1 | wc -l
    138  12:42   du -sk * | sort -n | tail -2
    139  12:45   history | tail -12    

(The columns of output are the command number, the time I ran it and the command itself.)

You can use the up and down arrows to cycle through your history. If I hit the up arrow twice, I’d have my command #138. Once you’ve found the command you want, you can use the left and right arrow keys to move your cursor around in the command to edit it. Say I wanted command #138, but I wanted that sorted in reverse, so that the command will show me the two smallest files. I’d do: up-arrow up-arrow left-arrow a bunch of times until the cursor was sitting on the n type an “r" press Return (you don’t have to be at the end of the command line for it to take effect when you’re going through the history like this)

Note that the home/end/page-up/page-down keys won’t work for you here, since the scrolling terminal view will consume those keys before the command line sees them.

You can also search through your history rather than hitting up-arrow over and over and over again looking for a command you know you’ve used recently. Typing a Control-R (just like you’d do Command-R, but use the ctrl key instead of the Command/Apple/propeller key) When you do this, your command line mutates, from
[localhost:~] bork%


[localhost:~] bork% bck:
... where bck: stands for backward search.

You then type in some of your command, like you could type in sk, and the shell will display the most recent du -sk .... If that’s not what you want, type Control-R again to see the second most recent command, and so on.

You can cut and paste the text for commands (and command output as well) into other programs, like, or Project Builder for later retrieval if you wish. Newer versions of BBedit have a command-line shell worksheet (similar to the MPW shell, for the old-timers out there) where you can keep commands hanging around for later.

What else?

So, what’s some other fun stuff you can do?

You can use the command line for other stuff. For instance, you can get the uptime of your machine. That is, how long since your machine was last restarted. One of the nifty-keen things about Unix (and therefore Mac OS X) is that the system just doesn’t crash under normal circumstances. It’ll stay up and running until you explicitly tell it to shut down or reboot, leading to very long uptimes. So, when your Windows-running office mate announces another virus-related crash, you can say, “Gee, I haven’t had to reboot or quit Photoshop in four months.” My friend Doug’s desktop Linux machine at work had an uptime longer than his employment with a company. And this was at his first anniversary working there! So, running uptime looks like:

% uptime 1:03PM up 4 days, 16:12, 5 users, load averages: 0.39, 0.23, 0.04

This is the current time, the number of user sessions logged in, and the load averages of the machine (an indication of how hard the machine is working). The three numbers are averages of machine load over the last 5, 30, and 60 seconds. High numbers mean the machine is doing more work. Very high load numbers can indicate a performance problem or an overloaded system.

The terminal window can run interactive programs as well. Try running the top command like this:

% top -u
Processes:  46 total, 3 running, 43 sleeping... 148 threads
Load Avg:  1.20, 0.53, 0.34     CPU usage:  65.9% user, 30.1% sys, 4.1% idle
SharedLibs: num =  144, resident = 14.5M code, 676K data, 3.72M LinkEdit
MemRegions: num = 4089, resident =  105M + 3.11M private, 31.3M shared
PhysMem:  46.0M wired,  146M active, 20.6M inactive,  213M used,  299M free
VM: 2.13G + 66.7M   237494(0) pageins, 37450(0) pageouts

  6164 Terminal    68.3%  4:05.71   8   133   618  4.41M  6.57M  9.34M 63.7M
  6713 top         10.5%  0:01.16   1    15    14   228K   244K   468K 1.37M
    70 Window Man   7.3%  3:48:19   3   171   493  2.18M  12.8M  14.7M 39.1M
     0 kernel_tas   5.6% 44:10.03  26     0     -      -      -  34.4M 399M
  6548 TruBlueEnv   2.4%  6:02.60  17   171   198  18.2M  7.64M  25.1M 1.05G
  6186 Mozilla      1.6%  9:48.85   9   129   778  65.0M  3.91M  64.5M 146M
   280 Dock         0.0%  9:34.29   3   103   157  1.57M  4.55M  3.22M 54.7M
   280 Dock         0.0%  9:34.29   3   103   157  1.57M  4.55M  3.22M 54.7M
   279 Finder       0.0%  1:41.64   3   134   518  5.97M  10.5M  13.3M 83.9M
    68 ATSServer    0.0%  0:43.36   1    34    98   440K  1.40M  1.45M 15.3M
    72 update       0.0%  0:37.78   1     8    13    12K   204K    72K 1.25M
   272 slpd         0.0%  0:36.36   8    29    28    84K   296K   264K 4.99M
   210 AppleFileS   0.0%  0:24.68  12    95    49   148K   332K   416K 8.76M
   270 loginwindo   0.0%  0:18.83   7   150   146  1.74M  2.48M  2.90M 43.0M
  6576 emacs        0.0%  0:16.69   1    13    21  1.79M  2.96M  3.38M 10.3M

That’s a lot of stuff. And if you let it keep running, you’ll see that it updates itself. This is a peek into the guts of your system to see what it is doing and how healthy it is. The “man page” for top (I’ll speak about that later) tells you what most everything means. The main piece of interest is the lower two-thirds of the output which shows the programs currently running sorted by how much processor time (CPU) they are consuming (that’s what the -u flag does). It’s actually a useful diagnosis tool – if your machine is sluggish, you can run top to see who the CPU pig is.

To exit top, press q.

Next page: In Unix, you need never feel lost! (No, really.)

E-mail this story to a friend