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
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
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
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 questionmark
less pin filename
less command with multiple blank line file
less -s filename
less file1 file2
less multiple file accessing second file
less -x filename
less +F filename
sudo dmesg less