Learning Unix by ExampleMany Unix commands are available, and there is no way you can do more than scratch the surface in this small chapter. However, you can learn how Unix commands work in general by trying some specific examples of useful Unix commands.
For references in which you can learn more Unix commands, p. 260. Each of the following sections provides information about specific commands. For each command, you will see four areas of information about that command. First, you will read a general description of what the command does. Second, you will see the command's syntax and some of the useful options for that command. Third, you will see a more specific description of the command's effect. Fourth, you will see the steps you can take to use the command. NOTE For the commands in this section, you won't see all or even many of the options that are possible for each command. You will need to access a more detailed reference for that type of information, such as the command's manual pages. Learning About the EnvironmentWhen you are troubleshooting, it can be helpful to understand the environment in which you are running Unix. You can use the uname command to get information about the computer on which you execute the command. Or, you might need to check this information to make sure some software or hardware is compatible with your system:
Type env and press Return. You will see information including your Home directory, the shell you are running, the username you are using, the language being used, the application you are using to enter Unix commands, and so on. This information can get pretty extensive; scroll up the window to just below where you entered the command to see the more meaningful parts. TIP At the top of the scrollbar in the Terminal window is a broken square. If you click this, the Terminal window splits into two panes, and you can work within each pane independently. This is useful when you want to view two areas of the window at the same time but can't expand the window large enough to be able to do so. Viewing the Contents of DirectoriesYou will frequently need to move up and down the directory structure to work with specific files or other directories. Unix has many commands that enable you to do so, including
Use the pwd command when you aren't sure about the directory in which you are currently located. When you use the command, you will see the full path in which you are working. This can be helpful if you become confused about where you are as you move around the directories.
NOTE When a specific command is listed in a step, you should ignore the period at the end of the command. For example, in the following steps, don't type the period after the command cd Music in step 1.
NOTE In a pathname, the tilde character (~) indicates that you are in your Home directory. In the step 2, you could have just entered cd ~ to move back into your Home directory.
NOTE Although most commands and options are in lowercase, they aren't always. For example, the -F option is different from the -f option (both are valid for the ls command).
TIP If you type the command ls la, you will also see the invisible files in a directory. The permissions string you see at the start of each item in the full listing indicates how the item can be accessed. The first character indicates whether the item is a file (-) or a directory (d). The next three characters indicate what the owner of the file can do; r is for read, w is for write, and x is for execute. If any of these characters is the hyphen (-), that action can't be taken. The next three characters indicate the permission that the group has to the file. For example, if these characters are r-x, other members of the group can read, not write, and execute the file. The last three characters indicate what everyone else can do. The execute permission applies to a directory. To access a directory, you must have both read and execute permission. If you also have w permission, you can change the contents of the directory as well.
Type file, followed by the filename you would like information about, and press Return. Information about the file is displayed. Changing the Contents of DirectoriesYou can use Unix commands to change the contents of directories as well. For example, you can delete files using the rm command. This can sometimes be faster than using the Trash. Once in a while, you might not be able to use the Trash to get rid of a file; you can often use the Unix commands to accomplish the task when other means fail.
NOTE You can't remove the current directory unless you enter the full path to it. TIP The asterisk (*) is a wildcard character. For example, to delete all the files in a directory that have the file extension .tiff, you can type rm *.tiff.
Type mv filename path and press Return. The file or directory filename is moved to the location path.
Using the ManualAll Unix commands have a manual associated with them. This manual lists the syntax for the command and defines its options; manuals can be a good reference when you are using a specific command but can't remember an option or the command's exact syntax. Many manual pages also provide some explanation about how the command works.
It is a good idea to take a look at the manual pages for any Unix commands you use. Pay special attention to the list of options that are available for the command. NOTE Some Unix applications provide manual pages using the help argument. For example, perl --help brings up information about the Perl application. TIP Pressing the spacebar moves you down the manual page one screen's worth at a time; you can move down a manual page one line at a time by pressing the Return key instead. Using Superuser CommandsAs you learned earlier in the book, the root account is the fundamental user account that can do anything under Mac OS X. The root account has more access to the system than even an administrator account does. Using this account can be hazardous to your system because, when you are under root, the OS assumes that you know what you are doing and doesn't provide any checks on your activities. You can easily delete things you don't mean to or mess up the system itself. CAUTION By entering the root account, you can do damage to your system. You should use this only when you really have to, and even then, you need to be very careful about the commands you enter while you are working on the root prompt. However, when you need to use a specific command at a specific time that you can't do under another user account, it can be helpful to enter commands as root.
For help activating the root account and creating a password for it, p. 236.
TIP To return to the previous account, type exit and press Return. Killing a ProcessWhen a process goes wrong, it can cause problems, such as hanging, or it might start consuming tremendous amounts of processing power, thus bringing your system's performance to a crawl. You can tell that a process has gone out of control by monitoring its percentage of CPU usage. If this number gets high and stays there, the process is likely hung. Because the information in the top window is dynamic, you should open it in a Terminal window and then open another window to enter commands. Under Mac OS X, there are several ways to stop an out-of-control process. For applications, you can use the Force Quit command. At the process level, you can use the Process Viewer to force a process to quit. You can also use the powerful Unix command kill to stop a running process.
NOTE If the process you are trying to kill is an Administrator process, you will have to use the sudo -s command to get into the root account before you use the kill command. |