Linux 101 Tutorial - Viewing the Contents of Text Files

Introduction

This section will demonstrate how to view the contents of text files in the the Linux Terminal. Many tools are available in the Terminal to achieve this, however note that some may be more suited to a specific situation than others. As you continue using the Terminal, you will naturally learn which tools are more effective in particular circumstances.

Navigate to Tutorial/nvt_expr_1 view the the file hpc.debug:

$ cat hpc.debug

The cat command is useful for shorter files, as shown in the example above, but may be cumbersome when dealing with longer files. For example, view the contents of nvt_expr_1.log using cat:

$ cat nvt_expr_1.log

View the contents of text files using more

Using more allows one to scroll down through a text file (using spacebar key). Use more on nvt_expr_1.log:

$ more nvt_expr_1.log

View the contents of text files using less:

To be able to scroll up and down in a text file, use the command less. Use the less command on nvt_expr_1.log

$ less  nvt_expr_1.log

Additionally, advanced navigation can be performed with less. Lets explore this by performing the following actions:

  • Move to the end of the file by typing G
  • Move back to the start of the file by typing g
  • Move to the 500th line by typing 500
  • Search for the term "PME" by typing /PME
  • Now move to the next match by typing n
  • Move back to the previous match by typing N
  • Finally, quit back to the terminal by typing q

Tip

If you master regular expressions, you may use a regular expression pattern to search for text. This is extremely powerful.

View the start of text files using head

Relevant information may be at the start of a text file. To specifically view information at the start of a text file, use the command head to display a number of lines at the start of the file.

Display the first 100 lines of the nvt_expr_1.log file:

    $ head -n 100 nvt_expr_1.log

View the end of text files using tail

At other times relevant information may be at the end of a text file. To specifically view information at the end of a text file, use the command tail to display a number of lines at the end of the file.

Display the last 100 lines of the nvt_expr_1.log file:

    $ tail -n 100 nvt_expr_1.log