A Command Line with the Mac OS?

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 that are available. It also means that 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 with the commands in order 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 there aren't that many situations 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 you with 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 chapter. 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 that you can learn more about using Unix if you choose to.

Terminal

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.

Figure 9.1. The command-line interface in Terminal isn't much to look at, but it is very powerful.

graphics/09fig01.jpg

NOTE

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.


Shells

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 be different). 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 tcsh. Other shells are available, but tcsh is a good place to get started.

NOTE

You can install other shells on your Mac in order to work with them. After you download and install the shell, you use the Shell pane of the Terminal's Preferences window 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.


Unix Command Structure

To enter commands, you type them at the prompt. All Unix commands use a specific syntax and consist of the following three parts:

  • Command

  • Options

  • Argument

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.

NOTE

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 by a semicolon as in command1; command2. Each command will be activated in the order in which you list them.

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, which 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 only need to enter 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 of the current directory, you must type the full path. In Unix, full paths always start with /.

NOTE

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, I 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 get 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 will be 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.

  1. In a Finder window, open the Home directory for your user account.

  2. Open a new Terminal window from within the Terminal application by choosing File, New Shell. A new Terminal window will appear.

    TIP

    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.

  3. Drag the Documents directory from the Finder window onto the new Terminal window. The path to the directory will be shown at the prompt (see Figure 9.2).

    Figure 9.2. You can quickly enter a path at the prompt by dragging an item from a Finder window onto the Terminal; in this case, I dragged the Documents folder from my Home directory onto the Terminal window.

    graphics/09fig02.jpg

NOTE

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 on using 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. 218.

Unix Applications

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. 226.

Shell Scripts

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 chapter. See the references listed at the end of this chapter for information about creating and using shell scripts.

Unix Flavors

Finally, you should be aware that there are different versions of Unix. And, there are different releases of different versions 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.



Special Edition Using Mac OS X v10. 2
Special Edition Using Mac OS X v10.2
ISBN: 0789729040
EAN: 2147483647
Year: 2005
Pages: 260
Authors: Brad Miser

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