-->
Previous Table of Contents Next


If you try to delete a directory with the rm command, you’re told that it’s a directory and can’t be deleted. If you want to delete empty directories, use the rmdir command, as with MS-DOS.

Linux offers another way to delete directories and their contents, but it’s far more dangerous. The rm -r command recursively deletes any directories and files it encounters. If you have a directory named ./foo that contains files and subdirectories, the command rm -r foo deletes the ./foo directory and its contents, including all subdirectories.

If you give the command rm -i -r, each directory that the rm command encounters triggers a confirmation prompt. You must answer yes before the directory and its contents are deleted. If you left any files in the directory you were attempting to delete, rm balks, just as it does if you attempt to remove the nonempty directory with the rm command with no options.


NOTE:  You don’t have to issue each flag individually for a Linux command. If the flag doesn’t take an argument, you can combine the flags. Thus, rm -i -r can be issued as rm -ir.

Viewing the Contents of a File

Almost every Linux command prints to the standard output device, typically your screen. If the command takes its input from a file after manipulating the file in some way, the command prints the file to your screen. The trick in choosing a Linux command depends on how you want the file displayed. You can use three standard commands: cat, more, and less.


NOTE:  Linux, as all UNIX systems do, opens four system files at startup: standard input, standard output, standard error, and AUX. These files are actually physical devices:
Name Alias Device
Standard input standard in (stdin) The keyboard
Standard output standard out (stdout) The screen
Standard error standard err (stderr) The screen
AUX auxiliary An auxiliary device

Using cat to View a File

For displaying short ASCII files, the simplest command is cat, which stands for concatenate. The cat command takes a list of files (or a single file) and prints the contents unaltered on standard output, one file after another. Its primary purpose is to concatenate files (as in cat file1 file2>file3), but it works just as well to send the contents of a short file to your screen.

If you try to display large files by using cat, the file scrolls past your screen as fast as the screen can handle the character stream. One way to stop the flow of data is to alternatively press <Ctrl-s> and <Ctrl-q> to send start and stop messages to your screen, or you can use one of the page-at-a-time commands, more or less.

Using more to View a File

Both more and less display a screen of data at a time. Although they both do roughly the same thing, they do it differently. more and less determine how many lines your terminal can display from the terminal database and from your TERM environment variable.

The more command is older than less, and it’s derived from the Berkeley version of UNIX. It proved so useful that, like the vi editor, it has become a standard. This section covers just the basics of the command.

The simplest form of the more command is more filename. You see a screen of data from the file. If you want to go on to the next screen, press the space bar. If you press <Return>, only the next line is displayed. If you’re looking through a series of files (with the command more file1 file2) and want to stop to edit one, you can do so with the e or v command. Pressing <e> within more invokes whatever editor you’ve defined in your EDIT shell environment variable on the current file. Pressing <v> uses whatever editor has been defined in the VISUAL variable. If you haven’t defined these variables in your environment, more defaults to the ed editor for the e command and to the vi editor for the v command.


See “Setting the Shell Environment,” p. 344

The more command has only one real drawback—you can’t go backward in a file and redisplay a previous screen. However, you can go backward in a file with less.

Using less to View a File

One disadvantage to the less command is that you can’t use an editor on a file being displayed. However, less makes up for this deficiency by allowing you to move forward and backward through a file.

The less command works almost the same way that more does. To page through a file, type the command less filename. One screen of data is displayed. To advance to the next screen, press the Spacebar as you did with the more command.

To move backward in a file, press the <b> key. To go to a certain position expressed as a percentage of the file, press <p> and specify the percentage at the : prompt.

Searching Through a File and Escaping to the Shell

The less and more commands allow you to search for strings in the file being displayed. The less command, however, allows you to search backward through the file as well. Use the search syntax less /string to search backward through the file. With the less and more commands, if a string is found, a new page is displayed with the line containing the matching string at the top of the screen. With less, pressing the <n> key repeats the previous search.

The more and less commands also allow you to escape to the shell with the ! command. When you escape to the shell with the ! command, you’re actually in a subshell; you must exit the subshell just as you do when you log out from a session. Depending on which shell you’re using, you can press <Ctrl-d> or type exit to return to the same screen in more or less that you escaped from. If you press <Ctrl-d> and get a message to use logout instead of <Ctrl-d>, use the logout command.


Previous Table of Contents Next