Using the Command-Line Interface


In a graphical interface, programs are controlled primarily by selecting and clicking menus and windows with buttons and text fields. As such, you can usually discover what a program is capable of doing just by examining the onscreen options. A command-line interface executes individual programs, called commands or tools, that you enter. A command consists of the name of the command followed by any options (often called switches) that you choose to provide, and any arguments for the command. Note that command names are case-sensitive. To execute a typed command, press the Return key.

The command prompt is the starting point for entering commands in a command-line interface. It can be daunting to stare at the command prompt if you're used to a graphical interface because there are no helpful onscreen hints suggesting what to do.

The command prompt indicates where you arethe name to the left of the colon (:) is the name of the computer. (If localhost is shown instead of the name of the computer, it is because a reverse DNS lookup could not be done on the computer's IP address.) The information to the right of the colon shows the working folder you are in. The tilde character (~) is an abbreviation representing your home folder.

Immediately following the working folder are the name of the currently logged-in user and a separator character (%, $, or #). If the separator character # is shown, it indicates that the current user is running commands as the System Administrator.

In this screen shot, the user named owen is logged in on a computer named iMac and is working in his home folder.

NOTE

The prompt is specific to each shell and is also user-configurable. What we show here is the default prompt you get with bash (Bourne Again Shell), the default shell.


NOTE

Traditionally, UNIX users and UNIX commands use the word "directory" to describe the file system structure that can contain other file system items. This book follows the Mac OS X convention of using the word "folders" to describe these file system structures.


You can access the command line in Mac OS X in the following six ways:

  • Terminal For the remainder of this lesson, you will use Terminal (/Applications/Utilities) when you access the command line because it is the most convenient and secure method.

  • Remote login using ssh (secure shell) or Telnet You can log in to a remote Mac OS X computer from any computer by using ssh, provided that you use the user name and password of a user account on the remote Mac OS X computer. After you ssh into a remote Mac OS X computer, commands entered on your computer are executed on the remote computer as though you were using it locally.

    There are two considerations for ssh login. The first is that Remote Login on Mac OS X is disabled by default. An administrator user can enable Remote Login in the Services pane of Sharing preferences. The second is that FileVaultenabled home folders must be mounted on that computer if you wish to access them across the network.

    NOTE

    Terminal includes a Connect to Server command that simplifies connecting to a remote computer. In Terminal, choose File > Connect to Server. In the Connect to Server dialog, select the connection method, such as ssh, and the computer you are connecting to, enter the user name, and click Connect.


  • Single-user mode By pressing Command-S at startup until you see white text on a black background, you enter single-user mode. In single-user mode, you have access to the file system as the System Administrator without having to log in. You exit single-user mode by entering exit.

    NOTE

    Single-user mode is a security risk because it gives the user System Administrator access to most of the files on the system. Setting an Open Firmware password on the computer will prevent a user from entering single-user mode. For more information, refer to Knowledge Base document 106482, "Setting up Open Firmware Password Protection in Mac OS X 10.1 or later."


  • >console If you enter >console as the user name with no password in the Mac OS X login window, the Mac OS X graphical interface disappears, and you are prompted to log in from a command-line interface prompt. At this point, you are using Mac OS X solely from a command-line interface. If you enter exit at the prompt, you return to the Mac OS X login window.

    If you log in via >console, you will enter a user environment very much like Terminal. The only user process running is the BSD shell. If you need to use a processor-intensive BSD application, >console is the best way to run that application without other tasks taking up processor cycles.

    NOTE

    Logging in with >console in effect enters a single BSD shell environment, with no graphical interface or other user processes active. As such, it should only be run when Fast User Switching is not enabled.


  • Terminal from the Mac OS X Install DVD This is a new feature in Mac OS X 10.4. Start up from the DVD, then choose Utilities > Terminal.

  • X11 The optional X11 application environment has an application similar to Terminal that can be used to execute BSD commands.

    Unless otherwise noted, when this course mentions the command line, it refers to the interface and commands you will see in Terminal.

Entering Commands

Each BSD command is entered on a single line after the shell prompt and in the following format:

command option(s) (arguments)

For instance, the ls command lists the contents of the named folders. Adding an option to this command gives you a little more information in the listing. Entering ls -lA ~/Documents lists the contents of your Documents folder along with their permissions for files and subfolders. Regardless of where you are in the file system, ~ refers to your home folder.

Many shell commands operate on or produce textual information. Shell commands that require input typically read their input from the standard input device (the keyboard), and commands that generate output typically write to the standard output device (the monitor).

Input/output redirection allows you to change the input device, the output device, or both for a given command. For example, you might want to redirect the output of an ls command to a file for inclusion in an email message. Such a command might look like this:

ls -lA ~/Documents > ~/Desktop/lstext.txt

The greater-than sign is used after the ls command (with its options and argument) to redirect output to a text file identified by path and filename. In this case, the list of items in the Documents folder would be saved to a text file named lstext in the current user's Desktop folder.

Accessing Online Help

All UNIX-based systems provide online help using the man (for manual) command. This command formats and displays pages describing the command, configuration file, or other item. The man pages for a command contain:

  • The name

  • A brief synopsis

  • A description

  • Examples of command syntax and usage (for most, but not all, commands)

The man command, followed by a command name, displays the manual pages for that command. For instance, on the command line, enter

man ls

This command displays information about the ls command, including its many parameters.

When you view a manual page, you are actually using a utility called more, so you can use commands to navigate the man page contents. If you press Return, you will move ahead one line. If you press the spacebar, you will move ahead one page. The up arrow and down arrow keys move up and down one line, respectively. If you are looking for a specific word on the man page, you can enter /word and press Return, and your window will jump to the next instance of that word. Press Q to exit the man page and return to the command line.

You can also search the man pages for specific words using:

apropos keyword

where keyword is the specific word you're searching for. This command displays a list of commands whose man pages include this keyword. Using apropos is a good way to discover new commands.

NOTE

A newly installed system may not have had time to create the database used by the apropos command. You can force the update of the database by entering the following command:

sudo /etc/weekly


The man pages are organized in numbered sections. If there is more information to display than fits on one screen, press the spacebar to page down. Sometimes you have to specify the section number to find the page you want. For example, open is the name of a command, but it is also the name of a Perl language construct, and a UNIX system call. To see the man page for the Perl language construct, you need to use

man 3 open

To find out what a section contains, enter

man section intro

For instance:

  • Section 1 General commands (tools and utilities)

  • Section 2 System calls and error numbers

  • Section 3 C libraries

There are some things to keep in mind when you use man pages as references. Not all commands have man pages, and sometimes the man pages have errors. Some man pages automatically redirect to other man pages with similar functionality. Also, because some man pages are derived from open-source documentation, they may be inaccurate, incomplete, or out-of-date.

Viewing man Pages Using Terminal

You can use a command-line interface to view the man pages for UNIX commands.

1.

Open Terminal (/Applications/Utilities).

2.

At the command prompt, enter

man ping

and press Return to obtain information on the UNIX command ping that corresponds to the Ping pane in Network Utility.

You can continue viewing the rest of the documentation by pressing the spacebar.

3.

To quit viewing the documentation, press Q.

4.

View the man page for TRaceroute, which corresponds to the Traceroute command used in Network Utility.

man traceroute

Using Edit Keys

Text entered at the command line must be exact, and some file systems require you to enter case-sensitive filenames. Fortunately, most shells, including those included by default with Terminal, provide several keyboard shortcuts that make entering and editing commands easier.

Tab Completion

Tab completion allows you to type just the first unique letters of a command or path. When you press the Tab key, the shell will complete the string. When you type a partial command and press the Tab key, but there are other commands or paths that start with the same letters, the shell will respond with a beep and wait for more input.

For example, suppose you want to list the contents of your Documents folder. You begin by typing

ls ~/D

At that point, you press the Tab key and the shell beeps because there are two folders that start with D: Desktop and Documents.

If you continue by typing o and then press the Tab key again, the shell completes the folder name to

ls ~/Documents/

because there is only one folder in your home folder that begins with "Do."

Using the Finder for Paths

A quick way to enter a very long path is to drag the folder from the Finder into the Terminal window. The path appears after the cursor on the command line, with the correct path to that folder.

Shells interpret spaces in filenames as command, switch, or argument separators. To get around this, you can either put the entire path in single or double quotes, or precede each individual space with a backslash (\). When a character is preceded by a backslash, it is called "escaping the character." This prevents the shell from doing anything special with the character. When a shell performs filename completion for you, it will escape spaces in filenames.

Reusing Prior Commands

The shell maintains a list of previously entered commands. The up arrow and down arrow traverse the history list of commands. To execute a command again, press Return when it is visible at the command prompt.

Editing Commands

Shells often provide a number of ways to speed the editing of commands. Use Control-A to move quickly to the beginning of a line. Control-E moves the cursor to the end of the line.

In addition to using the cursor keys to move the cursor forward and backward on the command line, Control-F moves forward one character and Control-B moves backward one character. To move forward and backward one word at a time, use Esc-F and Esc-B, respectively.

The cursor does not need to be at the end of the line when you press Return. The shell interprets all of the text entered on the current line regardless of the cursor's location when you press Return.

Miscellaneous Shortcuts

Use Control-L or the clear command to clear the screen.

Control-C terminates many commands in progress and will cause the shell to ignore any text currently being entered and return you to the command prompt.

The following table summarizes many of the command-line entry shortcuts and actions:

Shortcut

Action

Tab

Completes the word being typed

Drag folder to Terminal

Enters the path name

Up and down arrow keys

Accesses prior commands

Control-A

Moves the cursor to the beginning of the line

Control-E

Moves the cursor to the end of the line

Control-F

Moves forward one character

Control-B

Moves backward one character

Esc-F

Moves forward one word

Esc-B

Moves backward one word

Control-C

Terminates the command in progress

Control-L or "clear"

Clears the screen





Apple Training Series Mac OS X Support Essentials
Apple Training Series: Mac OS X Support Essentials v10.6: A Guide to Supporting and Troubleshooting Mac OS X v10.6 Snow Leopard
ISBN: 0321635345
EAN: 2147483647
Year: 2003
Pages: 233

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