Linux less Command: File Paging

LinuxLinuxBeginner
Practice Now

Introduction

In this tutorial, you will learn how to use the less command in Linux to efficiently navigate and analyze large log files. The less command is a powerful tool for viewing text files in the terminal, allowing users to scroll through content, search for specific information, and view file contents page by page.

Imagine you are a system administrator tasked with investigating a series of server errors. You have access to a large log file containing information about system events, but the file is too big to open in a regular text editor. This is where the less command becomes invaluable.


Skills Graph

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

Accessing the Log File

First, let's navigate to the directory containing our log file and view its contents.

  1. Open your terminal. You should be in the /home/labex/project directory by default.

  2. List the contents of the directory:

ls

You should see a file named server_log.txt.

  1. To view the contents of this file using the less command, type:
less server_log.txt

This command opens the server_log.txt file using less. You can now view the contents of the file.

Note: The content you see may differ from the example below, as the log file is dynamically generated.

2023-11-05 08:00:01 INFO: Server startup complete
2023-11-05 08:15:23 WARNING: High CPU usage detected
2023-11-05 08:30:45 ERROR: Database connection failed
2023-11-05 08:31:02 INFO: Retrying database connection
2023-11-05 08:31:05 INFO: Database connection established
...

To exit the less view, press the q key.

Now that we have opened the file, let's learn how to navigate through it.

  1. Open the log file again using less:
less server_log.txt
  1. Use the following keys to navigate:
    • Press Space or Page Down to move forward one page
    • Press b or Page Up to move backward one page
    • Use the Up and Down arrow keys to move line by line
    • Press G (Shift + g) to go to the end of the file
    • Press g to go to the beginning of the file

Try these navigation commands to familiarize yourself with moving through the file.

  1. When you're done exploring, press q to exit less.

Searching for Specific Content

As a system administrator, you often need to find specific information quickly. The less command provides powerful search capabilities.

  1. Open the log file again:
less server_log.txt
  1. To search for the word "ERROR", type /ERROR and press Enter. This will highlight all occurrences of "ERROR" in the file.
  2. Press n to move to the next occurrence of "ERROR", or N to move to the previous occurrence.
  3. Now, let's search for a specific date. Enter yesterday's date, such as today is 2021-09-01, so you can search for "2021-08-31". Type /2021-08-31 and press Enter.
  4. Use n and N to navigate between occurrences of this date.
  5. When you're done searching, press q to exit less.

Displaying Line Numbers

When analyzing logs, it can be helpful to see line numbers for reference.

  1. Open the log file with line numbers displayed:
less -N server_log.txt

The -N option tells less to display line numbers.

  1. You should now see line numbers at the left side of each line:
     1 2023-11-05 08:00:01 INFO: Server startup complete
     2 2023-11-05 08:15:23 WARNING: High CPU usage detected
     3 2023-11-05 08:30:45 ERROR: Database connection failed
     4 2023-11-05 08:31:02 INFO: Retrying database connection
     5 2023-11-05 08:31:05 INFO: Database connection established
...
  1. Navigate through the file as before. Notice how the line numbers help you keep track of your position in the file.

  2. When you're done, press q to exit less.

Starting from a Specific Pattern

Sometimes, you may want to start viewing the file from a specific point. The less command allows you to do this easily.

  1. Let's say you want to start viewing the file from the first occurrence of a database error. Use this command:
less +/ERROR:.*Database server_log.txt

This command tells less to open the file and immediately jump to the first line containing "ERROR:" followed by "Database".

  1. You should now see the file opened at the first database error:
2023-11-05 08:30:45 ERROR: Database connection failed
2023-11-05 08:31:02 INFO: Retrying database connection
2023-11-05 08:31:05 INFO: Database connection established
...
  1. You can continue to navigate through the file from this point as before.

  2. When you're done, press q to exit less.

Summary

In this tutorial, you learned how to use the less command to efficiently navigate and analyze large log files. You practiced opening files, scrolling through content, searching for specific information, displaying line numbers, and starting from a specific pattern in the file.

These skills are crucial for system administrators and developers who often need to quickly analyze large log files to troubleshoot issues or monitor system performance.

Other useful less parameters not covered in this tutorial include:

  • -i: Ignore case in searches
  • -F: Quit if the entire file can be displayed on one screen
  • -S: Chop long lines instead of wrapping them
  • +F: Keep reading the file, displaying new contents as they appear (similar to tail -f)

Other Linux Tutorials you may like