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.

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.NOTETerminal 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.NOTELogging 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.

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, enterman ls This command displays information about the ls command, including its many parameters.

- 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.
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 typingls ~/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 tols ~/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 |