Hack 48 Introducing the Terminal

figs/moderate.giffigs/hack48.gif

This brief tour of the Terminal introduces you to some of the more basic commands required to find out where you are, move about, manipulate files and directories, and get back out again when you've had enough.

This brief tour of the Terminal assumes you're either an old-time Mac hand who's been thanking your lucky stars you've never been near a command-line interface (CLI) or a recent Windows switcher who's been previously scared away by the complexity or unimpressed by the functionality of the rather ill-equipped DOS shell. It is meant as a quick-start guide, introducing you to some of the more basic commands required to find out where you are, move about, manipulate files and directories, and get back out again when you've had enough. Come on in, the water's fine!

48.1 Launching the Terminal

To invoke the Terminal, choose Applications Utilities Terminal, as shown in Figure 5-1.

Figure 5-1. Launching the Terminal
figs/xh_0501.gif

A few Dock bounces later and you'll have a fresh Terminal window in which to work (see Figure 5-2). The Terminal informs you about the date of your last visit and welcomes you to Darwin, the Unix core of Mac OS X.

Need another Terminal window? Simply click File New Shell or figs/command.gif-N and another will make itself available to you.

Figure 5-2. A fresh Terminal window
figs/xh_0502.gif

All that [Apple-s-Computer:~] weimenglee% jazz is known as the prompt and provides some useful information about your current working environment. The bit before the : is your computer's name (set in the System Preferences Sharing pane). After the : is your current path, or whereabouts on your hard drive. In this case, I'm in my home directory, referred to coloquially as ~ (that's a tilde, found on the top left of your keyboard); were I in the Applications folder, my location would read as :/Applications. The bit just before the % is your username weimenglee, in my case.

You'll be issuing all your commands at the prompt, with the cursor that black block keeping track of your typing in much the same way the I-beam does in your text editor.

Typing fingers ready? You're ready to issue your first command.

48.2 Current Working Directory

Let's make sure we know where we are, shall we? Type pwd, short for "print working directory":

[Apple-s-Computer:~] weimenglee% pwd /Users/weimenglee

Unless you've gone anywhere since opening your Terminal window, you should be in your home directory, /Users/login, where login is your Short Name [Hack #1] on the system. Again, this is the same as the ~ shortcut.

That's all well and good, but where exactly is /Users/weimenglee with respect to the folders in the more familiar Finder? The screenshot in Figure 5-3 should help you to get your bearings.

Figure 5-3. The current working directory in the Finder
figs/xh_0503.gif

While in the Finder we have folders, in Terminal we call them directories. Subdirectories are delimited by a / (forward slash) character. Switchers, note that Windows uses \ (backslash) to delimit subdirectories, as in c:\mydocu~1. Remember to use / on the Mac command line.

Backslash, under Unix, has magical properties of its own. It's used to escape or call out special characters like spaces, question marks, or the like. You'll most likely use it for dealing with files containing spaces on the command line, letting Unix know that you're still talking about the same file and haven't moved on to another. Notice the semantic difference between the file this\ is\ one\ file versus file1 file2 file3.

48.3 List Files and Folders

Now that you know your whereabouts, let's take a gander at the content of the current directory. The ls (list) command displays the content of a particular directory:

[Apple-s-Computer:~] weimenglee% ls Desktop Documents Library Movies  Music   Pictures  Public  Sites

The ls command in this example displays the content of the current directory. You can also ask ls to list the contents of a specific directory and display the result in a particular format by passing it command-line options and a directory name.

In the Terminal, commands are in the format Command -options parameter. Options are prefixed by a - (hyphen); when there are more than one, they're pushed together.

As shown in Figure 5-4, ls -al Documents asked ls to list all (-a) the files using a longer (-l) listing format in the Documents directory.

Figure 5-4. Output generated by ls -al
figs/xh_0504.gif

By default, files beginning with . (dot) will not be displayed by ls. To display them, use the -a option. The two files listed with names . and .. are special files known as the current and parent directory, respectively.

Occasionally, you may have a long file listing, with output flowing off the top of the screen. To page through, one screenful at a time, send known as piping because of its use of the | (pipe) character the output to the more command:

[Apple-s-Computer:~] weimenglee% ls -al | more

48.4 Clearing the Screen

After trying out these comamnds, your screen will no doubt be full of files and directories. To clear the screen, type clear or press Control-L.

48.5 Changing Directories

To move about, issue a cd, or change directory, command, specifying a directory as the parameter. This is akin to opening a folder in the Finder. For example, let's meander over to the Public directory:

[Apple-s-Computer:~] weimenglee% cd Public [Apple-s-Computer:~/Public] weimenglee% 

Notice how the current directory the bit after the : in your prompt changes to ~/Public. This is a constant reminder of where you are at any moment in time; no need to keep typing cwd to find out. Remember that the ~ refers to your home directory; so, in this case I'm actually in /Users/weimenglee/Public.

There are two ways of specifying a particular directory. The first is to use the absolute or full path (e.g., cd /Users/weimenglee/Public). The second, much shorter when you're moving down the path relative to your current location, is to use the relative path (e.g., cd Public). Assuming you're in your home directory, these examples are equivalent.

Let's now turn our attention to the contents of the Public folder:

[Apple-s-Computer:~/Public] weimenglee% ls Drop Box

Inside of Public, there is one subdirectory, Drop Box, and no files. Change to the Drop Box directory by typing cd Drop\ Box. You can also use tab completion to save yourself a little typing; simply type cd D and press the Tab key. Bingo! The directory name is automatically completed for you. This works on both files and folders, relative and absolute paths. If there's another file or folder with the same initial letter, type the second letter and press Tab and so on, typing as much of the name necessary to distinguish it from others.

48.6 Moving On Up

To move up one step in the directory hierarchy, use .. to refer to the special parent directory:

[Apple-s-Computer:~/Public/Drop Box] weimenglee% cd .. [Apple-s-Computer:~/Public] weimenglee% 

Move up multiple levels by combining .. and /, like so:

[Apple-s-Computer:~/Public] weimenglee% pwd /Users/weimenglee/Public [Apple-s-Computer:~/Public] weimenglee% cd ../.. [Apple-s-Computer:/Users] weimenglee% pwd /Users/

cd . will have no effect, changing the current directory to, well, the current directory. But . will come in handy in a moment when we start copying files.

To go to the top of the directory (known as the root directory), use / all by itself:

[Apple-s-Computer:/Users] weimenglee% cd / [Apple-s-Computer:/] weimenglee% 

To return to your home directory, simply use the cd command with no parameters, the equivalent of cd ~ and cd /Users/login (where login is your Short Name):

[Apple-s-Computer:/] weimenglee% cd [Apple-s-Computer:~/] weimenglee%

48.7 Creating Directories

To create a new directory, use the mkdir (make directory) command, followed by the directory name either relative or absolute path. Note that if your new directory name contains spaces, you need to escape them or enclose the entire directory name in "" (double quotes). Otherwise mkdir will think that you mean to create multiple directories, as the following failed attempt to create a new folder called Temp Folder shows:

[Apple-s-Computer:~] weimenglee% mkdir Temp Folder [Apple-s-Computer:~] weimenglee% ls -al total 0 drwxr-xr-x 6 weimengl staff 204 Dec 11 08:50 . drwxr-xr-x 13 weimengl staff 442 Dec 10 17:58 .. -rw-r--r-- 1 weimengl staff 0 Dec 9 17:08 .localized drwxr-xr-x 2 weimengl staff 68 Dec 11 08:50 Folder drwxr-xr-x 2 weimengl staff 68 Dec 11 08:50 Temp

Either of the following two versions will work as expected:

mkdir Temp\ Folder mkdir "Temp Folder"

48.8 Removing Directories

To remove a directory, use rmdir (remove directory), the polar opposite of mkdir. The space issue applies as expected; either of the following will do:

rmdir Temp\ Folder rmdir "Temp Folder"

48.9 Copying Files

To copy a file, use the cp (copy) command, followed by the file to copy and its intended destination. Use either a relative or absolute path for each. For example, let's copy the file index.html from the directory Sites to Documents:

[Apple-s-Computer:~] weimenglee% cp Sites/index.html Documents [Apple-s-Computer:~/Documents] weimenglee% cd Documents [Apple-s-Computer:~/Documents] weimenglee% ls index.html

To copy a file to the current directory, use the special . filename, like so:

[Apple-s-Computer:~/Documents] weimenglee% cp Sites/index.html .

48.10 Deleting Files

To delete a file, use the rm (remove) command. The following example deletes that index.html we just copied to Documents:

[Apple-s-Computer:~/Documents] weimenglee% rm index.html

48.11 Moving Files

To move a file from one directory to another, use the mv (move) command, followed by the space-separated name and destination path. The following example moves the file index.html from the directory Sites to Documents:

[Apple-s-Computer:~] weimenglee% mv Sites/index.html Documents [Apple-s-Computer:~] weimenglee% cd Sites/ [Apple-s-Computer:~/Sites] weimenglee% ls images [Apple-s-Computer:~/Sites] weimenglee% cd ../Documents [Apple-s-Computer:~/Documents] weimenglee% ls index.html

The mv command is also used for renaming files. The following example renames the file from index.html to index.txt:

[Apple-s-Computer:~/Documents] weimenglee% mv index.html index.txt [Apple-s-Computer:~/Documents] weimenglee% ls index.txt

Let's put everything back, shall we? Type mv index.txt ~/Sites/index.html, and all should be as it was when we started this ride.

48.12 Viewing the Content of a Text File

At times, you may want to take a quick peek at the contents of a text file. To do so, use cat (concatenate), specifying the file or files to display, like so:

% cat .lpoptions Default _192_168_254_149

48.13 Copy and Paste, Drag and Drop

The standard editing suite, select all, copy, and paste, works as expected in the Terminal, whether invoked with figs/command.gif-A, figs/command.gif-C, and figs/command.gif-V or pulled down from the Edit menu.

A nice bit of interaction between command line and GUI is the ability to drag a file, directory, or bookmark from anywhere you may be in the Finder right onto the command line. Want to edit a file in a Terminal-based editor [Hack #51] without navigating the directory hierarchy to get to it? Type pico (or the like), followed by a space, and drag the file right into the Terminal window. It's a shortcut that comes in handy more often than you'd think.

48.14 Consulting the Manpages

There is only so much I can cover in this quick tour of the Terminal. You'll encounter a plethora of commands and applications on the command line. Whenever you need any help, try consulting the manual. Simply type man (as in manual, not oh, man!), followed by the command name. Your average manpage looks something like Figure 5-5.

Figure 5-5. The average manpage
figs/xh_0505.gif

48.15 Getting Off the Command Line

At any point you can always close the Terminal window as you would any other. It is far more polite not to mention cleaner to log out of the shell session you're running by typing exit or logout:

% logout [Process completed]

Wei-Meng Lee and Rael Dornfest



Mac OS X Hacks
Mac OS X Hacks: 100 Industrial-Strength Tips & Tricks
ISBN: 0596004605
EAN: 2147483647
Year: 2006
Pages: 161

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