8.9 Viewing Text Files

When searching for the contents of a file with grep the output gives you the line in which the searchstring was found along with the filename. Often this contextual information is still not enough information to decide whether you want to open and edit this file. Bash offers you several commands to have a quick look at the contents of a text file directly in the shell, without opening an editor.


With head you can view the first lines of a text file. If you do not specify the command any further, head shows the first 10 lines of a text file.


The tail command is the counterpart of head. If you use tail without any further options it displays the last 10 lines of a text file. This can be very useful to view log files of your system, where the most recent messages or log entries are usually found at the end of the file.


With less, display the whole contents of a text file. To move up and down half a page use PgUp and PgDn. Use Space to scroll down one page. Home takes you to the beginning, and End to the end of the document. To end the viewing mode, press Q.


Instead of less, you can also use the older program more. It has basically the same function—however, it is less convenient because it does not allow you to scroll backwards. Use Space to move forward. When you reach the end of the document, the viewer closes automatically.


The cat command displays the contents of a file, printing the entire contents to the screen without interruption. As cat does not allow you to scroll it is not very useful as viewer but it is rather often used in combination with other commands.