Underneath its candy-coated Aqua interface, Mac OS X is based on Unix, an operating system that dates back to 1969. Unix and its archetypal user interface, the command line, look alien to many Mac users. Instead of using graphic methods such as clicking and dragging, you interact with the command line via terse, cryptic text. The commands and responses may be bewildering at first, but even the most visually oriented user should consider learning this lingo–some tasks can be accomplished much more quickly in the command line, and some are possible
in the command line.
In OS X, you reach the command line through an application called Terminal. This article, a collection of tips, will introduce you to Terminal and some essential Unix commands. Future articles will build on this knowledge, but if you find you’re immediately thirsting for more, go to our
Mac OS X resource page.
Just keep in mind that the command line is serious business. Even experienced users occasionally wipe out entire directories (and their contents) by accident. It’s easy to overwrite existing files. And there is
no undo command
here. Always double-check what you’re about to do so you avoid mishaps.
Contributing Editor STEPHAN SOMOGYI thinks the addition of the command line to the Mac OS tool chest is a feature, not a bug. As with any power tool, though, appropriate protective gear should be worn at all times.
1: Find Your Way
Before you can manipulate files and folders, you need to know how to get to them. Instead of double-clicking on folders to look inside, you have to tell the command line which folders to go to and which files to enumerate.
When you launch Terminal, you’re greeted with a line of text that ends in a % (percent) character (A). This line of text, the
, indicates that the software that interprets your commands (the
) is ready.
Every time you launch Terminal, you start from your OS X Home directory. To see which files are in the Home directory, enter
–short for “list directory contents”–after the prompt, and press return.
In the traditional Mac OS, when you want to copy or duplicate a file, you first open its folder in the Finder. To do the same using Terminal, enter
–short for “change directory”–in the command line. If you have a subfolder named “clam” in your home folder, entering
will change the current working directory to that subfolder. To then list its contents, type
If you want to move back up through the folder hierarchy, you can use
–two periods mean “the directory above” when used as a directory name. A single period means “the current directory.”
2: Manipulate Your Files
Now that you can examine your files via the command line, you’ll want to do things with them.
You use one command both to move files to other folders and to rename them:
mv, short for “move.” If you have a file named “pismo” that you want to change to “brighton,” type
mv pismo brighton
and press the return key. But first make sure you don’t already have a file by that name in the same location–if you do, it will be overwritten.
When you want to copy a file (rather than move or rename it), use the
command, which, like the
command, wants to know what you’re copying and where you’re copying it to.
Traditional Unix is case sensitive: if a file or folder is called “Ocean,” you must enter the capital
every time. Although the HFS+ file system used by Mac OS X isn’t as strict, using consistent capitalization is a good habit to get into, especially because OS X can mount volumes (either over the network or locally) whose file systems are case sensitive (for example, UFS).
3: Control Text Flow
Sometimes commands spew more lines of text at you than can fit on one screen. Fortunately, there are several ways to slow the onslaught and move through it.
command lets you move forward through reams of text one page at a time and also lets you move backward. This command is often handier than using Terminal’s scroll bar.
When you combine commands by using a
, you allow Unix to use one command’s output as a second command’s input. A pipe is represented by the
(vertical bar) character.
Say you want to list the contents of a directory that contains hundreds of files and folders. Enter only
ls, and many of those files and folders will scroll past too quickly to read. The solution is to use a pipe, along with
less, creating a code string that looks like this:
ls | less
This command tells the system to feed the result of
less, which then proceeds to display it one page at a time (B).
4: Wild Cards
Wild-card characters are among the most powerful–and most perilous–features of the command line because they allow you to affect many files with one command. In this example, I combined some now-familiar commands with wild cards. When you explore wild cards, try not to be too all-encompassing: use them cautiously, particularly when deleting files, so you don’t delete or damage files you care about.
When constructing commands, you frequently have to specify which file or files you want to affect. Instead of typing the name of each file, you can use a wild card to make the computer do the work. The
(asterisk) wild card basically translates to “anything.” Another useful wild card,
?, translates to “any single character.”
Suppose you have a folder called “bigtext” in your home directory; it’s filled with text files, and you want to examine the contents of those files.
From your home directory, enter
To view the contents of the files, enter
(A). Using a wild card this way displays every file in the current directory that has a
After you enter
less *.txt, press return. Though results will vary based on your files, you should see output similar to the example (B).
But say we want to see only those text files with a single-character name. Simple enough: enter
C, which displays any file with a name that ends in
and has only a single character before the period.
After you press return, you should see something like the output in the example (D).
You can also combine wild cards:
shows you the insides of all files ending in
, for example, but not those ending in
Many Mac files have spaces in their names, so it’s useful to know how to handle this situation on the command line as well. If you don’t show the shell that the space is part of the file’s name, it will treat the space as a file-name separator. When a space is part of a file name, put the whole thing inside single quotation marks.
less ‘Pearls ??.txt’
will display the contents of files named “Pearls 00.txt,” “Pearls 99.txt,” and any similarly named files in between.