To begin with, let's look at a long file. In fact, let's look at a file so long that it will not fit on your screen if you were to print out the contents of the file to your screen. The cat command (short for concatenate) does just this; it prints out the file to your screen. If, however, the file is long, then you will only see the end of the file on your screen. Remember the user denise from earlier in the book? She had a lot of files in her directory. Let's list the files in her directory and redirect the output to a file called listing with the following command: $ ls -a /home/denise > listing When we cat listing to the screen, we see only the end of the file, as shown in Figure 22-1. We know that this is the end of the file because I have issued cat with the -n option which includes line numbers. The line numbers indicate that this is not the beginning of the file. Figure 22-1. cat -n Command
Seeing only the end of this file is not what we had in mind. Using pg (for page), we see one screen at time, as shown in Figure 22-2: Figure 22-2. pg Command
The more command produces the same output as pg, as shown in Figure 22-3: Figure 22-3. more Command
This is more like it; now we can scroll on a screen-by-screen basis with both pg and more . However, sometimes you want to view only the beginning or end of a file. You can use the head command to view the beginning of a file and the tail command to view the end of a file. The following two examples of head and tail (see Figures 22-4 and 22-5) show viewing the first 20 lines of listing and the last 20 lines of listing , respectively: Figure 22-4. head Command
Figure 22-5. tail Command
The command you use depends on the information you wish to display. My personal preference, whether viewing the contents of a large file or a long listing of files, is to use more . I don't have a good reason for this, and all I can say is that we are creatures of habit and I have always used more . The following are command summaries for cat, pg, more, head , and tail . I included some of the most frequently used options associated with these commands. Because none of the commands are difficult to use, I suggest that you try each command and see whether one suits your needs better than the others. Here are summaries of the cat, pg (not available on all UNIX variants), more, head, and tail commands. cat - Display, combine, append, copy, or create files.
pg - Display all or parts of a file.
more - Display all or parts of a file one screen at a time.
head - Provide only the first few lines of a file.
tail - Provide the last few lines of a file.
|