Linux less Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the less command, a powerful text viewer for Linux. The lab covers understanding the less command, navigating through text files, and searching and highlighting text within the less pager. You will create sample text files and practice using various less commands to efficiently view and navigate through text content. This lab provides practical examples to help you become proficient in using the less command for basic file and directory operations in a Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/less("`File Paging`") linux/BasicFileOperationsGroup -.-> linux/more("`File Scrolling`") linux/BasicSystemCommandsGroup -.-> linux/man("`Manual Access`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") subgraph Lab Skills linux/cat -.-> lab-422758{{"`Linux less Command with Practical Examples`"}} linux/less -.-> lab-422758{{"`Linux less Command with Practical Examples`"}} linux/more -.-> lab-422758{{"`Linux less Command with Practical Examples`"}} linux/man -.-> lab-422758{{"`Linux less Command with Practical Examples`"}} linux/grep -.-> lab-422758{{"`Linux less Command with Practical Examples`"}} end

Understand the less Command

In this step, you will learn about the less command, a powerful text viewer for Linux. The less command allows you to view and navigate through text files efficiently.

First, let's create a sample text file that we can use for this exercise:

echo "This is a sample text file for the less command." > sample.txt

Now, let's use the less command to view the contents of the sample.txt file:

less sample.txt

Example output:

This is a sample text file for the less command.

The less command opens the file in a pager, allowing you to navigate through the text. Here are some basic navigation commands in less:

  • spacebar or pagedown: Move down one page
  • b or pageup: Move up one page
  • g: Move to the beginning of the file
  • G: Move to the end of the file
  • / followed by a search term: Search for the term in the file
  • n: Move to the next occurrence of the search term
  • q: Quit the less pager

The less command provides many more features and options, but these are the basic commands you'll use most often.

In this step, you will learn how to navigate through text files using the less command.

First, let's create a larger text file that we can use to practice navigation:

curl -o book.txt https://www.gutenberg.org/files/84/84-0.txt

This downloads the full text of "Alice's Adventures in Wonderland" by Lewis Carroll, which we can use to explore the navigation features of less.

Now, let's open the book.txt file using less:

less book.txt

You should see the contents of the book displayed in the less pager. Here are some commands to navigate through the file:

  • spacebar or pagedown: Move down one page
  • b or pageup: Move up one page
  • g: Move to the beginning of the file
  • G: Move to the end of the file
  • / followed by a search term: Search for the term in the file
  • n: Move to the next occurrence of the search term
  • ? followed by a search term: Search for the term in reverse
  • N: Move to the previous occurrence of the search term
  • h: Display the less help menu with all available commands

Try using these commands to navigate through the "Alice's Adventures in Wonderland" text. For example, you can search for a character's name or a specific quote from the book.

When you're done, press q to quit the less pager.

In this step, you will learn how to search for and highlight text within a file using the less command.

Let's continue using the book.txt file we created in the previous step. Open the file with less:

less book.txt

To search for a specific term, simply type / followed by the term you want to find. For example, to search for the word "Alice", type:

/Alice

This will highlight the first occurrence of "Alice" in the text. To move to the next occurrence, press n. To move to the previous occurrence, press N.

You can also search for a term in reverse by using ? instead of /. For example, to search for "Alice" in reverse, type:

?Alice

In addition to searching, less also allows you to highlight text. To highlight all occurrences of a term, use the -P option followed by a regular expression. For example, to highlight all occurrences of the word "Alice", use the following command:

-P'Alice'

This will highlight all instances of the word "Alice" throughout the text.

To remove the highlighting, simply press the ESC key.

Try experimenting with different search and highlighting techniques using the book.txt file.

Summary

In this lab, you learned about the less command, a powerful text viewer for Linux. You first understood the basic functionality of the less command, including how to navigate through text files by moving up and down pages, searching for text, and jumping to the beginning or end of the file. You then practiced these navigation techniques using a larger text file, the full text of "Alice's Adventures in Wonderland" by Lewis Carroll. The less command provides efficient ways to view and navigate through text files, making it a valuable tool for working with large or complex documents on the Linux command line.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like