Section 3.15. Listing the Contents of a File: catmoreheadtail


[Page 56 (continued)]

3.15. Listing the Contents of a File: cat/more/head/tail

To check the contents of the "heart" file that I had created in my home directory "/home/glass," I listed its contents to the screen using the cat utility. Notice that I supplied cat with the name of the file that I wanted to display:

$ cat heart    ...list the contents of the 'heart' file. I hear her breathing, I'm surrounded by the sound. Floating in this secret place, I never shall be found. $ _ 



[Page 57]

cat can actually take any number of files as arguments, in which case they are listed together, one following the other. cat is good for listing small files, but doesn't pause between screens of output. The more utility is better suited for viewing larger files, because it provides advanced facilities such as the ability to scroll backward through a file. Figure 3-16 gives some notes on each utility.

Figure 3-16. Description of the more command.

Utility: more -f [+lineNumber] { fileName }*

The more utility allows you to scroll through a list of files, one page at a time. By default, each file is displayed starting at line 1, although the + option may be used to specify the starting line number. The -f option tells more not to fold long lines. After each page is displayed, more displays the message "-- More --" to indicate that it's waiting for a command. To list the next page, press the space bar. To list the next line, press the Enter key. To quit from more, press the q key. To obtain help on the multitude of other commands, press the h key.


While we're on the topic of listing files, there are a couple of handy utilities called head and tail that allow you to peek at the start and end of a file, respectively. Figures 3-17 and 3-18 describe how they work.

Figure 3-17. Description of the head command.

Utility: head -n { fileName }*

The head utility displays the first n lines of a file. If n is not specified, it defaults to 10. If more than one file is specified, a small header identifying each file is displayed before its contents.


Figure 3-18. Description of the tail command.

Utility: tail -n { fileName }*

The tail utility displays the last n lines of a file. If n is not specified, it defaults to 10. If more than one file is specified, a small header identifying each file is displayed before its contents.


In the following example, I displayed the first two lines and last two lines of my "heart" file:

$ head -2 heart      ...list the first two lines I hear her breathing, I'm surrounded by the sound. $ tail -2 heart       ...list the last two lines Floating in this secret place, I never shall be found. $ _ 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

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