Viewing Files with cat, more, pg, head, and tail


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

graphics/22fig01.gif

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

graphics/22fig02.gif

The more command produces the same output as pg, as shown in Figure 22-3:

Figure 22-3. more Command

graphics/22fig03.gif

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

graphics/22fig04.gif

Figure 22-5. tail Command

graphics/22fig05.gif

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.

Options

 

-

Used as a substitute for specifying a file name when you want to use the keyboard for standard input.

 

-n

Line numbers are displayed along with output lines.

 

-p

Replace multiple consecutive empty lines with only one empty line.

 

-s

This is silent mode, which suppresses information about nonexistent files.

 

-u

Output is unbuffered, which means that it is handled character by character.

 

-v

Print most non-printing characters visibly.

pg - Display all or parts of a file.

Options

 

-number

The number of lines you wish to display.

 

-p string

Use string to specify a prompt.

 

-c

Clear the screen before displaying the next page of the file.

 

-f

Don't split lines being displayed.

 

-n

A command is issued as soon as a command letter is typed, rather than having to issue a new line character.

more - Display all or parts of a file one screen at a time.

Options

 

-c

Clear the screen before displaying the next page of the file.

 

-d

Display a prompt at the bottom of the screen with brief instructions.

 

-f

Wrap text to fit the screen and judge page length accordingly .

 

-n

The number of lines in the display window is set to n .

 

-s

Squeeze multiple consecutive empty lines onto one empty line.

head - Provide only the first few lines of a file.

Options

 

-c

The output is produced with a specified number of bytes.

 

-l

The output is produced with a specified number of lines. This is the default.

 

-n count

The number of bytes or lines is specified by count . You can also use -count to specify the number of bytes or lines, which is shown in the example. The default count is 10.

tail - Provide the last few lines of a file.

Options

 

-bnumber

Specify the number of blocks from the end of the file you wish to begin displaying.

 

-cnumber

Specify the number of characters from the end of the file you wish to begin displaying.

 

-nnumber

Specify the number of lines from the end of the file you wish to begin displaying. You can also specify a number or minus sign and number, as shown in the example, to specify the number of lines from the end of file to begin displaying.



HP-UX 11i Systems Administration Handbook and Toolkit
HP-UX 11i Systems Administration Handbook and Toolkit (2nd Edition)
ISBN: 0131018833
EAN: 2147483647
Year: 2003
Pages: 301

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net