Basic Utilities

 < Day Day Up > 

One of the important advantages of Linux is that it comes with thousands of utilities that perform myriad functions. You will use utilities whenever you use Linux, whether you use 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. Some 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. On other operating systems, including Windows, Macintosh, and frequently Linux GUIs, a directory is referred to as a folder. That is a good analogy: A directory is a folder that can hold files.

tip: In this chapter you work in your home directory

When you log in on the system, you are working in 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. Chapter 4 goes into more detail about directories.

ls: Lists the Names of Files

Using the editor of your choice, create a small file named practice. (A tutorial on vim appears on page 141 and a tutorial on emacs appears on page 198.) After exiting from the editor, you can use the ls (list) utility to display a list of the names of the files in your home directory. In the first command in Figure 3-1 ls lists the name of the practice file. (You may also see files 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 118 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 there is no such 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 page 31 for a tip on options) and the name of the file you want to delete,rm displays the name of the file and then waits for you to respond with y (yes) before it deletes the file. It does not delete the file if you respond with a string that does not begin with y.

 $ rm -i toollist rm: remove regular file 'toollist'? y 

Optional: You can create an alias (page 312) and put it in your startup file (page 83) so that rm always runs in interactive mode.

less Is more: Displaying a Text File One Screen at a Time


When you want to view a file that is longer than one screen, you can use either the less utility or the more utility. Each of these utilities pauses after displaying a screen of text. Because these utilities show one page at a time, they are called pagers. Although they are very similar, they have subtle differences. At the end of the file, for example, less displays an EOF (end of file) message and waits for you to press q before returning you to the shell. In contrast, more returns you directly to the shell. In both utilities you can press h to display a help screen that lists commands you can use while paging through a file. Replace the cat command in Figure 3-1 with less practice and more practice to see how these commands work. Use the command less /etc/termcap if you want to experiment with a long file. Refer to page 697 for more information on less.

tip: Filename completion

After you enter one or more letters of a filename (following a command) on a command line, press TAB and the shell will complete 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, press TAB again to display a list of possible completions. (Refer to "Pathname Completion" on page 308.)

The preceding description assumes you are running bash. Filename completion works a little differently if you are running tcsh; see "Word Completion" on page 350.

hostname: Displays the System Name

The hostname command displays the name of the system you are working on. Use this command if you are not sure that you are logged in on the right system.

 $ hostname 

     < Day Day Up > 

    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    A Practical Guide to LinuxR Commands, Editors, and Shell Programming
    ISBN: 131478230
    EAN: N/A
    Year: 2005
    Pages: 213 © 2008-2017.
    If you may any questions please contact us: