As you learned earlier in the book, Mac OS X is running on top of a version of the Unix operating system. This means that Mac OS X can use many of the Unix applications. It also means you can enter Unix commands directly in the command-line interface to manipulate your system. In fact, in some situations, using a Unix command might be the only way you can accomplish a task (such as deleting a rogue file that you can't delete by dragging it to the Trash).
Unix is a very powerful language/operating system; however, it is also enigmatic and many of its commands require you to use complicated syntax to get them to work properly. Unix commands are incomprehensible to most people by just looking at them, so don't expect to be able to figure out how a particular command works without some help. Mostly, you will learn about commands you want to use from various Unix resources (such as this chapter, other books, Web sites, and Unix manual pages). You might find using the command line to be so counter to the traditional Mac interface experience that you don't want to use it; if so, that is fine because few situations exist in which it is required in everyday Mac use. However, if you want to master Mac OS X, you should become familiar with the command line and learn some basic Unix commands. You might find that Unix provides ways of doing things that are both powerful and efficient.
There is so much you can do with Unix that there is no way you can learn how to work proficiently with it in the few pages of this chapter. To become even remotely fluent in Unix, you will need to do some additional learning outside of this book. What you can learn here is generally how the command-line interface works, and you can also learn how to use some basic Unix commands as examples. In the "Mac OS X to the Max" section at the end of the chapter, I provide references for you so you can learn more about using Unix if you choose to.
You use the Terminal application (Applications/Utilities) to enter Unix commands in the command-line interface (see Figure 9.1). The Terminal window is simple; all you see are your last login date and time, a Welcome message, the hostname, the user account under which you are logged in, and the command prompt.
Hostname is the name of the machine that is hosting your Unix session. When you are running a Unix session from your local machine, this will be localhost for most default configurations. If you are providing services over the network, the computer's network name is used.
In Unix, the shell is the user interface you use to interact with Unix. You can use different shells for the same set of Unix tools; each shell will have slightly different features, but they all work somewhat similarly (although the specific commands you use can differ). You can change the specific shell you use if you find one that offers features in which you are interested.
The default shell for working with Unix under Mac OS X is called bash. Other shells are available, but bash is a good place to start.
You can install other shells on your Mac to work with them. After you download and install the shell, you use the Terminal's Preferences to set the shell you want to use. The details of using different shells are beyond the scope of this chapter. See some of the references listed at the end of this chapter for help.
To enter commands, you type them at the prompt. All Unix commands use a specific syntax and consist of the following three parts:
The command is the specific action you want to take, such as listing the contents of a directory using the ls command.
You can enter options for that command to make the command work in a specific way. To add an option to a command, you type a hyphen followed by a letter. The options you can use are specific to each command. For example, when used with the ls (list) command, the -l option tells Unix to list the contents of the directory in the long format.
The argument is the "thing" on which the command will be executed, such as a file or directory. For example, to list the contents of a directory called mydirectory, the argument would be that directory name and the path to that directory.
When you enter specific commands, you might not use options or arguments; in some cases, you won't use either and will simply enter the command by itself.
Unix is case sensitive, so you must always follow the case conventions for specific commands. Most of the time, you will type everything in lowercase letters for commands and options, but paths can include both uppercase and lowercase letters.
You can run several commands in sequential order by separating the commands with a semicolon, as in command1; command2. Each command will be activated in the order in which you list it.
You can send the output of one command to be the input of another command by separating them with the pipe symbol (|), as in command1 | command2. This is called piping.
When entering commands, you will frequently need to use the path to a directory or file you want to manipulate. The path is the means by which you locate yourself in the hierarchy; levels of the hierarchy are indicated by the slash (/). Also, Unix uses relative pathnames. When you refer to something within or below the current directory, you need to enter only the portion of the path from the current directory to the subdirectories and files rather than the full path from the top level of the hierarchy. For example, to refer to a directory called mac_files within a directory called user_docs when you are currently in the user_docs directory, you would enter the path mac_files. When you want to move above or outside the current directory, you must type the full path. In Unix, full paths always start with /.
The full path to your Home directory is /macosx/Users/shortusername/, where macosx is the name of your Mac OS X startup volume and shortusername is the short username for your account. However, because you can use relative paths, you can leave out the first / and the name of your Mac OS X startup volume to get to this directory. You need to add only the volume name when you are working outside the current volume.
Unlike GUIs, Unix does not like spaces in filenames, volume names, or paths. To enter a space in one of these, use the backslash (\) followed by the space. For example, to refer to the volume called Mac OS X, you would enter /Mac\ OS\ X.
To get to the root of the startup volume, the path is simply /. However, unless you are logged on under the root account, you won't be able to do anything with the files and directories you see using a command line because of the system security.
One of the best ways to become familiar with entering pathnames is to drag items from a Finder window onto the Terminal window. When you do so, the pathname to that item is entered in the Terminal. You can use this trick to make entering paths easier because you can drag the item onto the prompt after you have entered a command and option to quickly add the argument to complete the command. And, after you drag several onto the window, you will get a good idea of how to type pathnames at the prompt manually. Follow these steps:
In a Finder window, open the Home directory for your user account.
Open a new Terminal window from within the Terminal application by selecting File, New Shell. A new Terminal window appears.
When using the Terminal, you can have multiple windows open at the same time. Each window is independent, so you can have multiple sessions running independently. You can save each session separately too, which you will learn about later in this chapter.
Drag the Documents directory from the Finder window onto the new Terminal window. The path to the directory is shown at the prompt (see Figure 9.2). (Note that you can't drag the folder from the Places sidebar; you must drag it from a Finder window.)
If you deal with Unix systems outside of Mac OS X, you will notice that paths almost never include spaces. Unix can have trouble properly interpreting spaces, so you can run into problems if the path you want to use includes spaces. Generally, if you plan to use Unix frequently, you should include underscores when you name your files and directories instead of spaces. Or, you can simply drag the object into the Terminal window and let the Mac enter the path for you. Spaces will be replaced by a backslash and a space (\ ).
To see examples of specific Unix commands, see "Learning Unix by Example," p. 248.
Because Unix has been around so long, thousands and thousands of Unix applications are available. You can run many of these under Mac OS X, and the OS includes several of these applications as part of the standard installation. For example, the Apache Web server application enables you to host your own Web pages. Mac OS X comes with a couple of Unix text editors, which are vi (Visual Editor) and emacs (an abbreviation of editing macros).
To get some examples of running these Unix applications, see "Working with Basic Unix Applications," p. 257.
You can invoke a series of commands using a Shell script; you can save the script and run it at any time to save yourself from having to retype the commands over and over. You create a script using the same syntax as in regular Unix commands. The difference is that you save those commands to a text file. When you want to run the commands, you execute the file instead. You can also run scripts that others have written just as easily.
The details of writing and running scripts are beyond the scope of this book. See the references listed at the end of this chapter for information about creating and using Shell scripts.
Finally, you should be aware that various versions of Unix are available. And, different releases of different versions exist as well. When it was released, Mac OS X included version 4.4 of the Berkeley Software Distribution (BSD) version of Unix. As this version is updated, the version that is part of Mac OS X will be updated as well.