Uncovering the Terminal (MacBook)

The best way to find out how to use command line is to jump right in. Mac OS X comes stocked with an application named Terminal. The Terminal application is where you enter commands in the command line. It’s located in the Utilities folder within the Applications folder on your hard drive — choose Applications Utilities.
Double-click the icon, as shown in Figure 1-1, to launch Terminal.
Find the Terminal application in your Utilities folder.
Figure 1-1:
Find the Terminal application in your Utilities folder.
tmp116-9_thumb
By the way, feel free to make Terminal more accessible by dragging its icon to the Dock or the toolbar. That way, you won’t have to dive this deep into the Applications folder in the future.


What’s a prompt?

Upon launch of the Terminal application, you’ll immediately notice some text in the window that appears on-screen:
Last login: Sun Jun 23 17:51:14 on console WHITEDRAGON:
As you might guess, this text details the last time that you logged into the Terminal. The last line, however, is the more important one. It’s called the prompt.
The prompt serves some important functions. First, it lists the current directory, which is listed as ~. A tilde character (~) denotes a user’s Home directory. By default, you’re always in your Home folder each time you begin a new session on the Terminal. After the current directory, the Terminal displays the name of the current user, which is in this example.
The final character of the prompt is a $. Consider this your cue because immediately after this character is where you enter any command that you wish to execute. Go ahead; don’t be shy. Try out your first command by typing uptime in the Terminal application. (It’s a good idea to type UNIX commands in lowercase.) Your text appears at the location of the cursor, denoted by a small square. If you make a mistake while entering the command, press the Delete key to back up and then type the characters again. (If the typing error is stuck deep in a longer command, press the left- or right-arrow key to move the cursor immediately after the incorrect character and press Delete to back up; then type the correct characters.) After you type the command, press Return to execute it.

WHITEDRAGON: uptime

6:24PM up 2:42, 4 users, load averages: 2.44, 2.38, 1.90 WHITEDRAGON
If all goes well, you should see a listing of how long your Mac has been running since the last reboot or login. In the example listing, the computer has been running for 2 hours and 42 minutes (2:42 in line 2). Simple, eh? Immediately following the listing of the uptime command, the Terminal displays another prompt for you to enter more commands. I examine many more commands later in this topic.
Prefer a different appearance for the Terminal window? Click the Terminal menu and choose Preferences; then click the Settings toolbar button to choose the color combinations for the Terminal window background and text.

A few commands to get started

Using the command line is simply a matter of entering simple instructions — or commands — into the Terminal application and pressing Return to execute them. It’s easy to use the command line to navigate through the various folders on your hard drive. You’ll become accustomed to using two vital commands: ls and cd. The ls command is shorthand for list, and it does just that: It lists the contents of the current directory. Enter ls at your prompt, and you should see a listing of your Home folder.
The complementary cd command (lowercase) — which incidentally stands for change directory — opens any folder that you specify. It works much the same as double-clicking a folder in the Finder: The difference is that following the cd
tmp116-10_thumb
command, you don’t immediately see all the folder’s content. However, the cd command requires a parameter (extra options or information that appear after the command) so that your Mac knows which folder to open.
For example, to open the Documents folder that resides in your Home directory, type cd Documents and press Return. When you do, you might be surprised to see another prompt displayed immediately. So where are all the files in the Documents folder? You must enter another command to see what items are in the folder that you just opened. Type ls again to see the contents of the Documents folder.
If you try to open a folder that has a space in its name, make sure to enclose the folder’s name in quotation  like this:
cd “My Picture Folder”
Read more about using quotation  in your commands in the upcoming section “Command-line gotchas.”
To return to your Home folder in this example, enter a modified version of the cd command:
cd ..
This causes your MacBook to move back up the folder hierarchy one folder, in this case to your Home directory. By using these three simple commands — ls, cd foldername, and cd .. — you can traverse your entire hard drive.
After you successfully enter a command, you can recall it by pressing the up-arrow key. Press the up-arrow key again to see the command prior to that, and so forth. This is an extremely useful trick for retyping extra long file paths.

Using the skills you already have

Just because the Terminal is text based doesn’t mean that it doesn’t act like a good Macintosh citizen. All the usual Mac features that you know and love are there for you to use. Copy and Paste functions work as you might expect — but only at the prompt position.
Drag-and-drop is also at your disposal. After you play around with the Terminal for a while, you’ll find yourself bored to tears typing the long paths that represent the files on your hard drive. To automatically enter the path of a file or folder to a command, simply drag it to the active Terminal window, as shown in Figure 1-2. The file’s full path instantly appears at the location of your cursor. (Thanks, Apple!)
Drag a file from a Finder window into Terminal to display its path.
Figure 1-2:
Drag a file from a Finder window into Terminal to display its path.
You can even use the mouse while entering commands in the Terminal. Click and drag your mouse over text to select it. From there, you can copy to the Clipboard as you might expect with any other application.

Next post:

Previous post: