One of the important advantages of Mac OS X is that it comes with hundreds of utilities that perform myriad functions. You will use utilities whenever you use Mac OS X, whether you access them directly by name from the command line or indirectly from a menu or icon. The following sections discuss some of the most basic and important utilities; these utilities are available from a character-based interface. Several of the more important utilities are also available from a GUI, and some are available only from a GUI.
The term directory is used extensively in the next sections. A directory is a resource that can hold files. In the Mac OS X graphical interface and in some other operating systems, a directory is referred to as a folder, which is a good analogy: A directory is a folder that can hold files.
Caution: In this chapter you work in your home directory
When you log in on the system, you are working in a directory that is called your home directory. In this chapter that is the only directory you use: All the files you create in this chapter are in your home directory.
ls: Lists the Names of Files
Using the editor of your choice (you can use a GUI editor or experiment with vim [tutorial on page 147] or emacs [tutorial on page 201]), create a small file named practice in your home directory. (In the standard Apple file dialog, the picture of a house with your name next to it represents your home directory.) After exiting from the editor, you can use the ls (list) utility (from the command linefor example, from Terminal) to display a list of the names of the files in the directory. The first command in Figure 3-1 shows ls listing the name of the practice file. (You may also see files that the system or a program created automatically.) Subsequent commands in Figure 3-1 display the contents of the file and remove the file. These commands are described next.
Figure 3-1. Using ls, cat, and rm on the file named practice
$ ls practice $ cat practice This is a small file that I created with a text editor. $ rm practice $ ls $ cat practice cat: practice: No such file or directory $
cat: Displays a Text File
The cat utility displays the contents of a text file. The name of the command is derived from catenate, which means to join together one after the other. (Figure 5-8 on page 124 shows how to use cat to string together the contents of three files.)
A convenient way to display the contents of a file to the screen is by giving the command cat, followed by a SPACE and the name of a file. Figure 3-1 shows cat displaying the contents of practice. This figure shows the difference between the ls and cat utilities: The ls utility displays the name of a file, whereas cat displays the contents of a file.
rm: Deletes a File
The rm (remove) utility deletes a file. Figure 3-1 shows rm deleting the file named practice. After rm deletes the file, ls and cat show that practice is no longer in the directory. The ls utility does not list its filename, and cat says that it cannot open the file. Use rm carefully.
Tip: A safer way of removing files
You can use the interactive form of rm to make sure that you delete only the file(s) you intend to delete. When you follow rm with the -i option (see the tip on page 29) and the name of the file you want to delete, rm displays the name of the file and asks you to respond with y (yes) or n (no) before it deletes the file.
$ rm -i toollist rm: remove 'toollist'? y
Optional: You can create an alias (page 311) for rm i and put it in your startup file (page 77) so that rm always runs in interactive mode.
By default rm does not act according to the Apple Human Interface Guidelines, which say that an operation should either be reversible or ask for confirmation. In general, UNIX utilities do not ask you whether you are sure of what you are doing.
open: Opens a File
The open utility launches a graphical application. For example, the command open memo.rtf starts TextEdit and loads the file memo.rtf. The application runs independent of the shell, which displays another prompt immediately. On a traditional UNIX system, the open utility would be difficult to implement, but Mac OS X provides a way for the system to tell, for many files, which application is best suited to opening them. For more information refer to "File Attributes" on page 95. Refer to page 803 for more information on open.
less: Displays a Text File One Screen at a Time
When you want to view a file that is longer than one screen, you can use the less utility. This utility pauses after displaying a screen of text. Because it shows one page at a time, it is called a pager. While you are using less, you can press h to display a Help screen that lists commands you can use while paging through a file. Give the command less practice in place of the cat command in Figure 3-1 to see how this command works. Use the command less /etc/services instead if you want to experiment with a longer file. Refer to page 768 for more information on less.
Tip: Filename completion
After you enter one or more letters of a filename on a command line, press TAB and the shell completes as much of the filename as it can. When only one filename starts with the characters you entered, the shell completes the filename and places a SPACE after it. You can keep typing or you can press RETURN to execute the command at this point. When the characters you entered do not uniquely identify a filename, the shell completes what it can and waits for more input. When pressing TAB does not change the display, you can press TAB again (Bourne Again Shell, page 306) or CONTROL-D (TC Shell, page 348) to display a list of possible completions.
hostname: Displays Your System Name
The hostname utility displays the name of the system you are working on. Use this utility if you are not sure that you are logged in on the right machine.
$ hostname bravo.local