Linux less Command: File Paging

LinuxLinuxBeginner
Practice Now

Introduction

This tutorial provides an overview of the less command in Linux, focusing on its functionality and practical usage.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicFileOperationsGroup -.-> linux/less("`File Paging`") subgraph Lab Skills linux/less -.-> lab-214301{{"`Linux less Command: File Paging`"}} end

less Command

The less command is a powerful tool for viewing text files in the terminal. It allows users to navigate through large files, search for specific content, and view file contents page by page.

Command Usage

Let's start with a basic example of using the less command. We have a file called example.txt in the /home/labex/project directory, and we want to view the contents of a file.

terminal

Input:

less /home/labex/project/example.txt

Output:

Line 1: This is a example text file.
Line 2: It contains multiple lines of text.
Line 3: You can use the 'less' command to view this file.
Line 4: Press the spacebar or enter to advance to the next page.
Line 5: Press 'q' to quit viewing the file.
2023-11-5 a
2023-11-5 b
2023-11-5 c
2023-11-5 d
2023-11-5 e
2023-11-5 f
2023-11-5 g
2023-11-5 h
2023-11-5 i
2023-11-5 j
2023-11-5 k
2023-11-5 l
2023-11-5 m
2023-11-5 n
2023-11-5 o
2023-11-5 p
2023-11-5 q
2023-11-5 r

This command opens the specified file (example.txt in this case) using less and displays its content. The tutorial will not cover additional parameters or advanced usage at this stage.

Parameters and Usage Examples

The less command offers a variety of parameters to customize its behavior. We will introduce some of the most commonly used ones and provide usage examples in increasing order of complexity.

Option Parameter

less [option] file

  • -N: Display line numbers.
  • +/pattern: Display a file from the line that containing specified pattern, and the pattern will be marked.

When you use the less command, press q to exit less, and Press the up and down keys to slide up and down the file

Example Usage

1. Display Line Numbers and File Information (-N)

In this example, we will use the -N option to display line numbers and file information.

Input:

less -N /home/labex/project/example.txt

Output:

## The -N option displays line numbers and file information
     1  Line 1: This is a example text file.
     2  Line 2: It contains multiple lines of text.
     3  Line 3: You can use the 'less' command to view this file.
     4  Line 4: Press the spacebar or enter to advance to the next page.
     5  Line 5: Press 'q' to quit viewing the file.
     6  2023-11-5 a
     7  2023-11-5 b
     8  2023-11-5 c
     9  2023-11-5 d
    10  2023-11-5 e
    11  2023-11-5 f
    12  2023-11-5 g
    13  2023-11-5 h
    14  2023-11-5 i
    15  2023-11-5 j
    16  2023-11-5 k
    17  2023-11-5 l
    18  2023-11-5 m
    19  2023-11-5 n
    20  2023-11-5 o
    21  2023-11-5 p
    22  2023-11-5 q
    23  2023-11-5 r

In this example, we will use the +/pattern option to search for the pattern "Press" in the file.

Input:

less +/Press /home/labex/project/example.txt

Output:

## The +/Press option starts displaying from the line containing the pattern "Press"
     4  Line 4: Press the spacebar or enter to advance to the next page.
     5  Line 5: Press 'q' to quit viewing the file.
     6  2023-11-5 a
     7  2023-11-5 b
     8  2023-11-5 c
     9  2023-11-5 d
    10  2023-11-5 e
    11  2023-11-5 f
    12  2023-11-5 g
    13  2023-11-5 h
    14  2023-11-5 i
    15  2023-11-5 j
    16  2023-11-5 k
    17  2023-11-5 l
    18  2023-11-5 m
    19  2023-11-5 n
    20  2023-11-5 o
    21  2023-11-5 p
    22  2023-11-5 q
    23  2023-11-5 r

In the examples above, less -N displays line numbers and file information, while less +/pattern starts displaying from the line containing the specified pattern.

Summary

In summary, the less command is a versatile tool for viewing and navigating through text files in the terminal. Whether you need to scroll through large files, search for specific content, or display line numbers, less provides a convenient and efficient solution for text file exploration.

Other Linux Tutorials you may like