Linux 101 - Viewing file contents in the Terminal

Introduction

At this stage the user should be able to navigate the Linux file system, while also being able to manipulate files and directories. The next step is to be able to view the contents of files. In most cases, only files containing text is human readable, however most files in the HPC research context are of this type. For example, log files generated by running jobs, parameter files to be supplied to programs running on the HPC and job submission scripts.

While reading text files in a dedicated editor is generally preferred (and cover here), it is useful to know the Terminal commands that can be used to quickly inspect the contents of text files in the terminal.

Thus, in this section we will explore these commands.

Please Note: The examples in this section will use this file. This is a log file generated by the biomolecular simulation software GROMACS. This log file presents a good example of how one would need to check different sections of the log file using the commands below (I.e. Actual use cases).

Display the contents of a file using cat

One of the basic functions of the cat command is to print out the entire contents of a file to the screen.

Common Use

Basic Syntax

$ cat *file_path*

Example

Print out the entire contents of the file gromacs.log :

viewfile_linux_1

Browse through the contents of a file using more

To browse through the contents in manageable chunks, the more command can be utilized. At the bottom of the screen, a percentage (%) indicator lets the user know how far they are in the document.

Common Use

Basic Syntax

$ more *file_path*

Example

Browse through the contents of the file gromacs.log :

viewfile_linux_2

Note: To advance through the contents, the user needs to press space, and q to exit.

Browse through the contents of a file using less

The command less functions in the same manner as the more command. The only difference is that less allows scrolling in both directions.

Common Use

Basic Syntax

$ less *file_path*

Example

Browse through the contents of the file gromacs.log :

viewfile_linux_3

Note: To advance through the contents, the user needs to press space, and q to exit like with the more command. The user can also enter the number of lines the next scrolling action will advance by. In the example, that is the 3 entered at the bottom of the screen.

View contents at the start of a file with head

In some instances you only need to inspect the header (top portion of the file) of a file. For example, with a GROMACS log file, the first part of the log file is inspected to check whether a submitted job have started.

In these instances, the command head can be used.

Common Use

Basic Syntax

$ head -n *number of lines to print*  *file_path*

Example

Print the first 20 lines of the file gromacs.log :

viewfile_linux_4

Note: The -n can be omitted. In that case a default of 10 lines will be printed to the terminal.

View contents at the end of a file with tail

In some situations it is necessary to inspect the end of a file. For example, with a GROMACS log file, the final part of the log file is inspected to follow the a simulation's progress while it's running, or to view the performance information of the run that is printed at the end.

In these cases the command tail can be used.

Common Use

Basic Syntax

$ tail -n *number of lines to print*  *file_path*

Example

Print the first 6 lines of the file gromacs.log :

viewfile_linux_5

Outcomes for this section

After completion of this section, you need to be able to do the following tasks in a Linux terminal:

  • Obtain information from any position in a text file