4.8. Viewing text files from the shell prompt

Red Hat Enterprise Linux has several applications that allow you to view and manipulate text files at the shell prompt. These applications are fast and best suited to manipulating the plain text files of configuration files.

4.8.1. The head Command

The head command displays the beginning of a file. The format of the head command is:

head <filename>

By default, you can only read the first ten lines of a file. You can change the number of lines displayed by specifying a number option.

head -20 <filename>

The above command would display the first 20 lines of a file named <filename>.

4.8.2. The tail Command

The reverse of head is tail. Using tail, you can view the last ten lines of a file. This can be useful for viewing the last ten lines of a log file for important system messages. You can also use tail to watch log files as they are updated. Using the -f option, tail automatically prints new messages from an open file to the screen in real-time. For example, to actively watch /var/log/messages, enter the folowing at a shell prompt (as the root user):

tail -f /var/log/messages

Press [Ctrl]-[C] when you are finished.

4.8.3. The more Command

The more command is a "pager" utility used to view text in the terminal window one page or screen at a time. The [Space] bar moves forward one page and [Q] quits.

4.8.4. Viewing Files with less

The format of the less command is:

less <filename>

The main difference between more and less is that less allows backward and single-line movement using the same navigation as man pages: press the [Space] bar to go down one page, the [B] to go back one page, the directional (or "arrow") keys to move one line at a time, and [Q] to quit.

To search the output of a text file using less, press [/] and enter the keyword to search for within the file.


The above command would search through the file for all instances of "stuff" and highlight them in the text.

4.8.5. Viewing and creating files with cat

The cat command is a versatile utility. It can be used to view text, to create text files, and to join files. Its name is short for concatenate, which means to combine files.

Using cat alone echoes on the screen any text you enter. It will continue to do so until you exit with the [Ctrl]-[D] keystroke.

Entering the cat command followed by a file name displays the entire contents of the file on the screen. If the file is long, the contents scroll off the screen. You can control this by using the redirection techiniques that are discussed in Section 4.9 Manipulating Information from the Shell.

4.8.6. The grep Command

The grep command is useful for finding specific character strings in a file. For example, to find every reference made to "pattern" in the file <filename>, enter:

grep pattern <filename>

Each line in the file that includes the pattern "pattern" is located and displayed on the screen/