Examining the contents of files
Last revision August 3, 2004
Table of Contents: |
We used pwd, cd, and ls to move around in the directory tree and examine the contents of directory files, which just contain lists of other filenames and the links to their inode numbers. What about looking at the contents of our data files, programs, papers, etc.?
more
The more program is the best program for displaying file contents on the screen. Give one or more filenames as arguments to more, which will display their contents in turn.
more knows what kind of terminal you are connected to, based on system files or your response to the terminal type prompt at login. It displays as many lines as will fit on one screen at a time, then prompts for more (showing percent of file viewed so far).
- To see the next screen full of lines, just press the SPACE bar.
- To quit the display, type the letter q (no RETURN key necessary).
You can scan the file searching for a line that contains a certain string of characters by typing a slash (/) followed by the string in response to the more prompt. In this case, you must end the string by pressing the RETURN key. The more command will then skip over the file contents until it finds that string, and then display a section of the file on the screen starting a few lines before the string. Read the on-line manual for other searching options.
Many other commands that need to display potentially long output to the screen send the output through the more command first, for example, msgs or man.
head
The head command shows only the first few lines of a file. By default, head shows only ten lines, but you can specify an alternate number with the -n option. You can give a list of files; it will show the first few lines of each in turn.
tail
The tail command is the companion to head, but it displays the last few lines of the file. Only works with a single file argument. Again, you can supply a count of how many lines to show; otherwise default is 10. tail also has additional options to get the last x bytes or blocks, or to display lines in reverse line order, which can work with multiple file arguments to concatenate them into reverse line order.
cat
The cat command can be used to display contents of any one or more files (concatenated) to the screen. Just shows entire file(s), without stopping.
Theoretically, you can control the scrolling of lines on the screen when cating a long file with the CTRL-S (stop scrolling - freezes display) and CTRL-Q (resume scrolling) keys. In reality, however, these rarely work when logged in over the network. This is because the output lines are packaged into large groups to be sent as network packets. The CTRL-S tells the Unix system to stop sending output, but by the time you press it, your workstation has usually received all the packets and just keeps displaying them.
Can use BREAK or CTRL-C key to abort the display. Again, network latency may mean that it takes a while for a response, with output continuing to the screen in the meanwhile.
Because of the difficulty of controlling the display with cat, it is generally not suitable for display of long files. It's main use is to concatenate several files into one, using output redirection.
<--Previous | Overview | |