Mac OS X Unleashed

Mac OS X Unleashed

By John Ray and William C. Ray

Interacting with Unix: Basic Unix Commands

You've already learned how to interact with Unix using OS X's Aqua interface and the GUI tools discussed throughout the first 11 chapters of this book. Much of the rest of the book will provide you with the information you need to interact with Unix textually, through the command line. Although there are no sharp dividing lines between Unix commands, Unix programs, and Unix applications, there is some benefit in making at least a fuzzy semantic distinction between them.

The Unix design philosophy drives programs that are used in day-to-day interaction with a Unix machine to be small, single-purpose, and nonoverlapping in functionality. The presence of a vast array of these single-purpose programs, designed so that they can be combined in near-infinite combinations, allows the user to construct customized solutions for most any problem. The necessity for some programs to provide more complicated functionality, requires them to be less-single purpose, and to allow somewhat more overlap. Finally, just as in other operating systems that you're used to, there are programs that are large, multifunctional, and monolithic. Typically, Unix users think of the small, single-purpose programs as commands, and the large, multifunctional programs as applic a tions. Although they're all programs, the term program is frequently reserved for a program that doesn't fit the description of a command or an application. This somewhat muddy semantic distinction between types of programs might seem confusing at first, but as you become more comfortable using Unix, it will make more sense to you. As an example to get you started, you can think of a Unix command as a small program with a single function such as listing files. A Unix application is typically a much larger program, perhaps something like a word processor or a Web browser. Moreover, although both are programs, the term program itself is infrequently used to describe anything that falls into either of these categories.

Thankfully for the beginning Unix user, making complete sense of the semantic distinctions isn't necessary for anything other than conversing with other Unix users. Unix commands, programs, and applications are all run by typing their names at the command line. The remainder of this chapter will cover what you need to know to start interacting with the command line, and the most basic Unix commands needed for day-to-day use.

Introduction to the Unix Shell

As mentioned previously, a terminal alone isn't sufficient to allow you to interact with your machine. The terminal needs something to talk to, and that thing is usually a program called a shell. Unix shells provide text-based interaction between the user and the rest of the operating system. For those who have experience with the DOS environment, you can think of a Unix shell as similar to a very, very, very powerful version of COMMAND.COM. Please don't let that put you off. COMMAND.COM does not approach being a fair comparison for a shell, but both do let you type commands to the computer.

Although it is text-based, you can think of a shell running in a terminal as sharing a few conceptual similarities with a Finder window.

Any running shell can be thought of as "being in a place" in the file system, just like a Finder window is open to a certain folder in the system. This place is the current working directory.

Each shell can navigate through the file system by moving to parent or higher-level directories, or by moving to child or lower-level directories. Again, this is much like the functionality provided by the folder menu in each Finder window and the folders displayed in the window.

Unlike a Finder window, a shell in a terminal is not restricted to running a command that is present in the same directory that the terminal is "at"—it can run commands that are located anywhere on the machine.

Also unlike Finder windows, shell commands that you execute in terminal windows (usually) run within the terminal window, and they (usually) consume the resources of the shell such that the shell becomes "preoccupied" and can't run another command until the current one finishes.

Most flavors of Unix come with a number of different shells from which the user can choose, and the BSD version underlying OS X is no exception. With OS X, you can choose from the following three shells, or possibly more, if Apple or another source makes them available.

Shell preference is just that: a matter of personal preference, although you'll find references and diatribes on the Internet that make the issue seem to take on almost biblical significance. Apple has chosen to make tcsh the standard shell for OS X, and this will satisfy a great many users because many do not like the syntax of sh. On the other hand, being able to perform day-to-day work in the same shell language that you'll need to use for the most ubiquitous distribution is a strong plus for bash.

Examples in this book will be shown using the default tcsh shell, but regardless of what shell you choose, interaction with each is similar. To issue a command to the computer using a shell, you type the name of the command and press the Enter key. In general, the command you requested will be executed by the shell, and will occupy the shell until it finishes, at which time you'll be returned to the shell prompt. If the command requires additional information, it might be required on the command line in the form of flags or arguments. Flags are usually individual letters or words preceded by a dash (-) that indicate the turning on or off of an option; arguments are words or data provided on the command line for the command to process. Some commands also require that data be provided at internal prompts or data entry areas created by the command when it is run. As mentioned previously, we'll discuss more complex programs and applications in following chapters.

Shell Rules and Conventions

In dealing with the shell, there are a few rules that it will help to remember. The first rule you'll need to know is things that you type in Unix are case sensitive. This includes commands. Unlike Mac OS and Windows, you cannot mix case and still have a command function. You must type the name exactly as it is stored on the system. Mac OS X has the option (the default) of using HFS+, which isn't case sensitive, but this creates some odd behavior. There are classical Unix commands included in the BSD subsystem that have names that differ only by capitalization, and that normally do different things. Because there's no obligation for OS X to be running on HFS+, and it's quite possible that you will be mounting drives from other machines that aren't running OS X at all, you really should treat everything in the BSD subsystem as though it were case sensitive, even if your installation actually isn't.

Next, when you type at the command line, characters that you type become part of the command. Most alphanumeric characters, as well as underlines and hyphens, are valid parts of commands. Most symbols aren't valid parts of commands, and some have special meanings to the command line. Table 12.2 shows some of the symbols with special meaning to the command line.

Table 12.2. Command-Line Symbols

Symbol Meaning
* When used as part of a filename, the * character will substitute for zero or more characters in a filename. This is called a wildcard. For example, specifying a filename of *.gif as an argument to a command-line program will tell the shell to search the current directory for all files that have names ending in .gif, and substitute all these filenames on the command line at this point in the command.
? A single character wildcard. Functions like *, except that it substitutes for a single character in filenames, instead of any number of characters.
Tab If you press the Tab key at the command line, the shell will attempt to complete the command for you. If the portion of the command or filename that you've typed is unique, the shell will fill in the rest of the information for you. For example, if you're in your home directory, and you want to specify the Documents directory on the command line, typing Docu[Tab-key] will most likely fill out the command line to include the full Documents name.
space Unix interprets a space between words on the command line as a separator between parts of the command. This is not always what you want because Macintosh filenames can have spaces. The Unix command line will usually interpret the space in a Mac filename such as My File as indicating two different files: one named My and one named File.
\ The shell escape character. If you need to insert a character into a Unix command or filename that is usually interpreted by the shell (like a space or * character), you can place the \ character before the character that would usually be interpreted by the shell. This is called escaping the special character. You will frequently see this on the command line to specify Mac filenames such as My File, which in Unix must be specified as My\ File.

Most useful shells provide a history mechanism, whereby previous commands can be recalled and reused. Previous commands may be recalled to the command line by use of the up-arrow key. If you need the last command you typed, just press the up-arrow key, and it will be recalled to the command line. You can rotate through the command history with the up-arrow and down-arrow keys to pick the command you need.

The command line also provides editing capabilities. You can use the left-arrow and right-arrow keys to move around in the command line currently displayed, and edit it by typing new characters or deleting existing characters with the Delete key. This also applies to commands that you've recalled via the command history with the arrow keys.

When you type a command, the system will search for a command with that name in the list of directories known as the PATH. This is done because a complete search of the entire file system could take a very long time, and restricting the portions of the file system examined to only a small subset speeds things up significantly. Unfortunately, the current directory, where the shell "is", isn't necessarily in the PATH. Because of this, you might be in a directory named /home/wizbot/spin/, and there might be a command named spinnin in the directory, but typing spinnin produces only the error command not found. In this case, you can run the command by specifying either the full name of the path to the command (/home/wizbot/spin/spinnin), or by specifying the relative path to the command (./spinnin). You can also solve the problem by adding the path to the directory holding spinnin to the PATH list, or by adding the current directory to the PATH. You'll learn about this and more in Chapter 18. You'll also learn considerably more about paths in the immediately following section, covering the file system and basic navigation.

Occasionally, you might do something that leaves your shell in an apparently unusable state. Frequently, this is because you've started a process that is expecting input from you. There are several key combinations you can try that might help you regain control of your shell:

Don't be afraid to test the commands shown in this book. The system can do a pretty good job of protecting itself from anything a normal user can type, and we'll be sure to place conspicuous warnings with any commands that are capable of causing mischief in any case. With a little experimenting, we think you'll find that you can do a surprising number of things with only a few keystrokes.

The Online Manual Pages

The very first command that you should familiarize yourself with is the man (manual) command. All good Unix systems provide an online collection of manual pages that detail almost every command available in the system. OS X is no exception. Simply type man <commandname> at any shell prompt, and if there is a manual page documenting the command, it will be shown to you. For example, to see the man pages for the time command (which, coincidentally, tells you what time it is), you could type

[localhost:~] miwa% man time

TIME(1)                   System Reference Manual                  TIME(1)

NAME
     time - time command execution

SYNOPSIS
     time [-lp] utility

DESCRIPTION
 The time utility executes and times utility. After the utility finishes,
 time writes the total time elapsed, the time consumed by system overhead,
 and the time used to execute utility to the standard error stream. Times
 are reported in seconds.

     Available options:

     -l      The contents of the rusage structure are printed.
     -p      The output is formatted as specified by.

   The csh(1) has its own and syntactically different builtin version of .
   The utility described here is available as /usr/bin/time to csh users.

DIAGNOSTICS
     The time utility shall exit with one of the following values:

     1-125   An error occurred in the time utility.
     126     The utility was found but could not be invoked.
     127     The utility could not be found.

     Otherwise, the exit status of time shall be that of utility.

SEE ALSO
     csh(1),  getrusage(2)

FILES
     /usr/include/sys/resource.h
/usr/share/man/cat1/time.0 (81%)

It is not unusual for Unix systems to be shipped with the man pages unformatted to conserve space, and OS X is no exception. Formatting the pages consumes a bit of disk space, but today's disk storage is cheap, and you get not only the benefit of faster display, but also a searchable man page system. To format your man pages, follow these instructions, and don't worry about the warning messages that you see. So long as the warning messages look like what is shown here, everything is working okay.

 [localhost:~] joray% su
Password:
[localhost:/Users/joray] root# catman /usr/share/man/man1

     catman: can't open /usr/share/man/man/: No such file or directory
     catman: can't open /usr/share/man/manu: No such file or directory
     catman: can't open /usr/share/man/mans: No such file or directory
     .
     <lots of errors deleted>
     .
     catman: can't open /usr/share/man/manm: No such file or directory
     catman: can't open /usr/share/man/mana: No such file or directory
     catman: can't open /usr/share/man/mann: No such file or directory
     mkdir /usr/share/man/cat1
     nroff -mandoc /usr/share/man/man1/a2p.1 > /usr/share/man/cat1/a2p.0
     nroff -mandoc /usr/share/man/man1/addftinfo.1 > cat1/addftinfo.0
     nroff -mandoc /usr/share/man/man1/afmtodit.1 > cat1/afmtodit.0
     .
     <lots of output nroff lines>
     .
     nroff -mandoc /usr/share/man/man1/zshmisc.1 > cat1/zshmisc.0
     nroff -mandoc /usr/share/man/man1/zshoptions.1 > cat1/zshoptions.0
     nroff -mandoc /usr/share/man/man1/zshparam.1 > cat1/zshparam.0
     nroff -mandoc /usr/share/man/man1/zshzle.1 > /usr/share/man/cat1/zshzle.0
     /usr/libexec/makewhatis /usr/share/man

[localhost:/Users/joray] root#

Next, run catman for each of the directories man4, man5, man6, man7, and man8 like so:

 [localhost:/Users/joray] root# catman /usr/share/man/man4

     .
     .
     .
[localhost:/Users/joray] root# catman /usr/share/man/man5

     .
     .
     .
[localhost:/Users/joray] root# catman /usr/share/man/man6

     .
     .
     .
[localhost:/Users/joray] root# catman /usr/share/man/man7

     .
     .
     .
[localhost:/Users/joray] root# catman /usr/share/man/man8

     .
     .
     .

Now that you've formatted your man pages, you can use the -k option to man. (This is equivalent to the apropos command; wherever you see man -k, you can substitute apropos, if that's easier for you to remember.) This option searches the man pages for pages with keywords matching what you've entered. For example, if you'd like to know which man pages might have information on the subject of time, you could issue a man command like this:

man -k time

Benchmark(3)             - benchmark running times of Perl code
CPAN::FirstTime(3), s-1CPAN:s0 - Util for s-1CPAN:s0:Config file Initialization
Net::Time(3)             - time and daytime network client interface
TCL_MEM_DEBUG(3)        - Compile-time flag to enable Tcl memory debugging
.
.
.
sleep(1)                 - suspend execution for an interval of time
strftime(3)              - format date and time
time(1)                  - time command execution
time(3)                  - get time of day
time2posix(3), posix2time(3) - convert seconds since the Epoch
times(3)                 - process times
timezone(3)              - return the timezone abbreviation
touch(1)                 - change file access and modification times
tzfile(5)                - time zone information
tzset(3)                 - initialize time conversion information
ualarm(3)                - schedule signal after specified time
uptime(1)                - show how long system has been running
utime(3)                 - set file times
utimes(2)                - set file access and modification times
vtimes(3)                - get information about resource utilization
zdump(8)                 - time zone dumper
zic(8)                   - time zone compiler

This command lists each of the man pages that the system knows about that match the keyword (in this case, time) that you've requested.

Finally, it's important to know that the man page system has several sections into which the content is divided. Because of this, there might be commands that have multiple man pages, each in a different section. The system is divided into sections roughly segregated into the following topics:

If you look at the earlier listing for the man -k time command, you will notice that each topic listed includes a number or letter in parentheses after the name of the topic. This is the man page section in which the item occurs.

To look at man pages that are in other sections of the manual, specify the numeric or character identifier for the section between the man command and the item you want to look up. For example, the system includes man pages for both the time command and the time() C library function. man time will default to showing you the man page for the command line–related item, if one exists, so both man time and man 1 time will show you the man page for the time command:

[localhost:~] joray% man 1 time

TIME(1)                   System Reference Manual                  TIME(1)

NAME
     time - time command execution

SYNOPSIS
     time [-lp] utility

DESCRIPTION
 The time utility executes and times utility. After the utility finishes,
 time writes the total time elapsed, the time consumed by system overhead,
 and the time used to execute utility to the standard error stream.  Times
 are reported in seconds.

     Available options:

     -l      The contents of the rusage structure are printed.

     -p      The output is formatted as specified by .

   The csh(1) has its own and syntactically different builtin version of .
   The utility described here is available as /usr/bin/time to csh users.

DIAGNOSTICS
     The time utility shall exit with one of the following values:

     1-125   An error occurred in the time utility.

     126     The utility was found but could not be invoked.

     127     The utility could not be found.

/usr/share/man/cat1/time.0 (70%)

If you want to know about the C library function named time(), however, you will need to look in the program-functions-related section of the man pages, using man 3 time as your command:

[localhost:~] joray% man 3 time
TIME(3)                 System Programmer's Manual                 TIME(3)

NAME
     time - get time of day

SYNOPSIS
     #include <time.h>

     time_t
     time(time_t *tloc)

DESCRIPTION
 The time() function returns the value of time in seconds since 0 hours, 0
 minutes, 0 seconds, January 1, 1970, Coordinated Universal Time.

 A copy of the time value may be saved to the area indicated by the point-
  er tloc. If tloc is a NULL pointer, no value is stored.

 Upon successful completion, time() returns the value of time.  Otherwise
 a value of ((time_t) -1) is returned and the global variable errno is set
 to indicate the error.

ERRORS
     The following error codes may be set in errno:

     [EFAULT]      An argument address referenced invalid memory.

SEE ALSO
     gettimeofday(2),  ctime(3)

HISTORY
     A time() function appeared in Version 6 AT&T UNIX.
/usr/share/man/cat3/time.0 (93%)

The man system is self-documenting, of course, so if you would like to read further about the man command, simply type man man.

The command documentation table for man is shown in Table 12.3.

Table 12.3. The command documentation table for man

man Displays online manual pages.
man [-achw] [-C <file>] [-M <path>] [-m <path>] [<section>] <name1>
<name2> ...

man [-M <path>] [-m <path>] -k <keyword1> <keyword2> ...
-a Displays all the manual pages for a specified section and name combination. (The default is to display only the first page found.)
-c Copies the manual page to the standard output instead of using more(1) for pagination.
-h Displays the SYNOPSIS lines of the requested manual pages.
-w Lists the pathnames of manual pages that would be displayed for the specified section and name combination.
-C <file> Uses the specified file instead of the default configuration file. This allows users to configure their own manual environment. See man.conf(5) for more details.
-M <path> Overrides the list of standard directories where man searches for manual pages. The path specified must be a colon-separated list of directories. The search path may also be specified by the MANPATH environment variable.
-m <path> Adds to the list of standard search directories. The path specified must be a colon-separated list of directories. These directories are searched before the standard list or directories specified by -M or MANPATH.
-k <keyword> Displays a list of manual pages that contain the <keyword> .
The optional <section> argument restricts man's search to the specified section.

Share ThisShare This

Informit Network