UNIX Commands 101 (MacBook)

To use the command line effectively, familiarize yourself with the commands that are available to you. After all, how can you use a tool without knowing what it can do? Despite having to memorize a few commands, UNIX usually makes it easy on you by abbreviating commands, by following a standard grammar (so to speak), and by providing you with extensive documentation for each command.

Anatomy of a UNIX command

UNIX commands can perform many amazing feats. Despite their vast abilities, all commands follow a similar structure:
command <optional flag(s)> <optional operand(s)>
The simplest form of a UNIX command is the command itself. (For a basic discussion on UNIX commands such as ls, see the earlier section, “A few commands to get started.”) You can expand your use of the ls command by appending various flags, which are settings that enable or disable optional features for the command. Most flags are preceded by a dash (-) and always follow the command. For instance, you can display the contents of a directory as a column of names (complete with file details) by tacking on a -l flag to the ls command.
ls -l
Besides flags, UNIX commands sometimes also have operands. An operand is something that is acted upon. For example, instead of just entering the ls command, which lists the current directory, you can add an operand to list a specific directory:
ls ~/Documents/myProject/
The tilde (~) denotes the user’s Home directory.
Sometimes a command can take multiple operands, as is the case when you copy a file. The two operands represent the source file and the destination of the file that you want to copy, separated by a space. The following example copies a text file from the Documents folder to the Desktop folder by using the cp command (short for copy).
cp ~/Documents/MyDocument ~/Desktop/MyDocument
You can also combine flags and operands in the same command. This example displays the contents of a specific folder in list format:
ls -l ~/Documents/myProject/


Command-line gotchas

In earlier sections, I describe a few simple command-line functions. All these commands have something in common: You might not have noticed, but every example thus far involved folder names and filenames that contained only alphanumeric characters. Remember what happens if you have a folder name that has a space in it? Try the following example, but don’t worry when it doesn’t work.
The cd command stands for change directory.
cd /Desktop Folder
The result is an error message:
-bash: cd: /Desktop: No such file or directory
The problem is that a space character isn’t allowed in a path. To get around this problem, simply enclose the path in double quotation  like this:
cd “/Desktop Folder”
Mac OS X lets you use either double or single quotation  to enclose a path with spaces in it. Standard UNIX operating systems, however, use double quotation  for this purpose.
tmp116-12_thumb
In a similar vein, you can get the space character to be accepted by a command by adding an escape character. To escape a character, add a backslash (\) immediately prior to the character in question. To illustrate, try the last command with an escape character instead. Note that this time, no quotation  are necessary.
cd /Desktop\ Folder
You can use either quotation  escape characters because they’re interchangeable.

Help is on the way!

By now, you might be wondering how a computer techno-wizard is supposed to keep all these commands straight. Fortunately, you can find generous documentation for nearly every command available to you. To access this built-in help, use the man command. Using the man command (shorthand for manual) will display a help file for any command that it knows about. For example, to read the available help information for the ls command, simply type man ls at the prompt. Figure 1-3 illustrates the result.
Use the man command to display help information.
Figure 1-3:
Use the man command to display help information.

Autocompletion

To speed things along, the bash shell can automagically complete your input for you while you type. Although the Terminal permits you to enter commands via the keyboard, it is the shell that interprets those commands. Many kinds of shells are available to UNIX users. The shell that Snow Leopard uses by default is bash — another common shell is tcsh. Use the autocompletion features of bash to autocomplete both commands and filenames. To demonstrate, begin by typing the following:
cd ~/De
Then press the Tab key. The result is that the shell predicts that you will want to type
cd ~/Desktop/
Of course, if you have another folder that begins with the letters De in the same folder, you might need to type a few additional characters. This gives the autocompletion feature more information to help it decide which characters you want to type. In other words, if you don’t type enough characters, autocompletion ends up like a detective without enough clues to figure things out.

Next post:

Previous post: