Unix Commands for Use in OS X Yosemite - dummies

Unix Commands for Use in OS X Yosemite

By Mark L. Chambers

To use the command line in OS X Yosemite effectively, familiarize yourself with Unix commands. After all, how can you use a tool without knowing what it can do? You might have to memorize a few commands, but Unix makes it easy on you by abbreviating commands, following a standard grammar (so to speak), and providing you with extensive documentation for each command.

Anatomy of a Unix command

Unix commands can perform many amazing feats. Despite their vast capabilities, all commands follow a similar structure. Note the spaces between the command, the flags, and the operands:

command <optional flag(s)> <optional operand(s)>

The simplest form of a Unix command is the command itself. 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 by tacking on a -l flag after the ls command.

ls -l

Besides flags, Unix commands sometimes also have operands, which are 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 using the cp command (short for copy) copies a file from the Documents folder to the Desktop folder.

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

What happens if you have a folder name with 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 /Example Folder

The result is an error message:

-bash: cd: /Example: 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 marks, like this:

cd "/Example Folder"

OS X lets you use either double or single quotation marks to enclose a path with spaces in it. Standard Unix OSes, however, use double quotation marks for this purpose.

You can get the space character to be accepted by a command by adding an escape character. (In this case, the escape character acts as a marker that skips over the space.) 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 marks are necessary.

cd /Desktop Folder

You can use either quotation marks or escape characters; they’re interchangeable. Note that the backslash () is the escape character, not the forward slash (/).

Command 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 access this built-in help, use the man command. Using the man command (short for manual) displays 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.

The General Commands Manual for OS X

Autocompletion

To speed things along, the bash shell can automagically complete your input for you while you type. A shell takes the commands you type and submits them to the OS, which then performs the tasks. 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 Yosemite uses by default is bash, and 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 Tab, and the shell predicts what you 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 because otherwise the shell returns the first hit it encounters. 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.