How to Use the Terminal in OS X Yosemite
The best way to find out how to use the Unix command line is to jump right into the Terminal application, which comes with OS X. Click the Launchpad icon on the Dock, click the Utilities folder, and then click the Terminal icon.
You can also jump directly to the Utilities folder from the keyboard by pressing Shift+Command+U (which displays the Utilities folder in a Finder window). Double-click the Terminal icon there. By the way, feel free to make Terminal more accessible by dragging its icon from the Utilities folder to the Dock.
Consider yourself prompted
Upon launch of the Terminal application, you’ll immediately notice some text in the window that appears onscreen, as shown in this example:
Last login: Sun Jun 23 17:51:14 on console WHITEDRAGON:~ markchambers$
This text details the last time that you logged in to Terminal. The last line, however, is the more important one: It’s the prompt.
The prompt serves some important functions. First, it lists the current directory, which is listed as ~ in the preceding example. 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 Terminal. After the current directory, Terminal displays the name of the current user, which is markchambers in this example.
As you’ve probably surmised, a folder and a directory are two different names for the same thing. Folder is the name with which most Mac users are familiar, and directory is a term that Unix power users prefer.
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 Terminal. This shows you a listing of how long your Mac has been running since the last reboot or login. (It’s a good idea to type Unix commands in lowercase because case-sensitivity is all-important in Unix.) Your text appears at the location of the cursor, denoted by a small square.
If you make a mistake while entering the command, no worries. Just press Delete to back up, and 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:~ markchambers$ uptime 6:24PM up 2:42, 4 users, load averages: 2.44, 2.38, 1.90 WHITEDRAGON:~ markchambers$
In the preceding example, 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 (line 3), the Terminal displays another prompt for you to enter more commands.
Prefer a different appearance for the Terminal window? Click the Terminal menu, choose Preferences, and click the Settings toolbar button to choose the color combinations for the Terminal window background and text.
A few commands to get started
As you use the command line to navigate through the various folders on your hard drive, you’ll lean on 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 the prompt, and you see a listing of your Home folder.
The complementary cd command (again, note the lowercase) — change directory — opens any folder that you specify. It works much the same as double-clicking a folder in Finder: The difference is that following the cd command, you don’t immediately see the entire folder’s content. Too, 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 in your Home directory, type cd Documents and press Return. Hmm. Another prompt is 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 your old friend ls again to see the contents of the Documents folder.
The preceding example works for a folder named with just one word. To open a folder with any spaces in its name, make sure to enclose the folder’s name in quotation marks, like this:
cd "My Picture Folder"
You can also precede a space (or any other character that the command line treats as special, such as an *) in a name with a backslash, like this:
cd My Picture Folder
To return to your Home folder in this example, enter a modified version of the cd command:
This causes your Mac to move back up the folder hierarchy one folder to your Home directory. By using these three simple commands — ls, cd foldername, and cd .. — you can traverse your entire hard drive (or at least those locations where you have permission to peruse).
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 mouse 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. The familiar Copy function works anywhere, as you might expect. Paste, however, works 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. The file’s full path instantly appears at the location of your cursor. (Thanks, Apple!)
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.