Looking at Text Files: More, Less, Head, and Tail

When working on the command line, you may come across files that are in a text format. In such cases, you may need to view the content of the file. There are various ways to view the content of a text file on the command line. In this section, we will look at four commands that can be used to view text files: more, less, head, and tail.


The more command

The more command is used to display the content of a file one page at a time. The more command will display as much of the file as can fit on the screen. To view the next page of the file, simply press the space bar. To quit the more command, press q.

Here's an example:


The less command

The less command is similar to the more command, but it allows you to scroll up and down through the file. You can also search for text within the file using the / key followed by the search term. To quit the less command, press q.

Here's an example:

The head command

The head command is used to display the first few lines of a file. By default, it will display the first 10 lines of the file. However, you can specify a different number of lines to display using the -n option followed by the number of lines you want to display. To display the first 20 lines of a file, for example, you would use the command head -n 20 filename.

Here's an example:


The tail command

The tail command is similar to the head command, but it displays the last few lines of a file instead of the first few. By default, it will display the last 10 lines of the file. However, you can specify a different number of lines to display using the -n option followed by the number of lines you want to display. To display the last 20 lines of a file, for example, you would use the command tail -n 20 filename.

Here's an example:

$ tail sample.txt
It contains some text that we can use to demonstrate how the tail command works.

The tail command is similar to the head command, but it displays the last few lines of a file instead of the first few.

To display a different number of lines, use the -n option followed by the number of lines you want to display.

$


Complete and Continue