Less command in Linux is used to read the contents of a text file one page at a time at the terminal. It is useful for reading large text file because it accesses the file page by page rather than loading the complete file. This results in faster loading and the file can be browsed line by line or section by section.

  • It allows users to navigate both forward and backward through the file.

In this tutorial, you will learn about less command and uses of less command with examples.

Syntax of less Command

less [options] filename

There are many options with less command, you can find all options available with less command using the command less --help

less --help

less --help
1. Open a txt file with less

Using the less filename command, The file’s first few lines are shown in the terminal. The file name and path are displayed in the bottom-left corner of the screen.

  • You may have trouble figuring out how to exit less, press ‘q’ to exit from less.
  • You may see empty blank lines at the top, if you try to display a short text file with less.
less filename

less filename
2. Display line number

You can display the line numbers in the less command. Use  -N option with less to show line numbers for the provided text file.

less -N filename

less -N filename
3. Find string

It may be a tough task to find any string or pattern in a large text file. less command allows us to search for a specific piece of text. To find any string or pattern press / (forward_slash) to initiate a forward search and type whatever you want to find.

less forward slash

less command forward slash search

less questionmark

less command search question mark

less pin filename

less -Psearch_text filename

less command with multiple blank line file

less filename command in Linux having blank line

less -s filename

less -s filename with a file having space

less file1 file2

less command multiple file

less multiple file accessing second file

less command multiple files accessing the second file

less -x filename

less -x filename

less +F filename

less +F filename

sudo dmesg less

sudo dmesg less

 

Sharing is Caring
Scroll to Top