In this article we are going to shows how to use less command in Linux Terminal. If you want to learn about less command in Linux with Examples then this post is ideal for you.
Less is a command line utility that is used to displays the contents of a file or a command output. It is read only one page at a time of a file or command output, so it is load faster compare to text editors like nano
or vim
.
Less command is similar to more
command but it has many advanced features and you can navigate to backward and forward through the file or output.
Less command in Linux
In the Linux, the basic syntax of the less command is show as below:
less [OPTIONS] filename
For an example:
$ less filename
When you run the above command it will display the content of the given file, one page at a time.
The less command is also allows you to redirect the output from a command to less
using a pipe.
$ ps aux | less
The above command display the output of the ps
command page by page on your terminal.
When you run the less command to print contents of a large file, then you will see a colon (:)
at end of your terminal screen.
Now If you like to read next pages then just press the f
key or space bar
using your keyboard. If you want to go to a specific line of a page then enter the number followed by space
or f
key.
Alternatively, you can press Enter
or Down arrow
key to scroll forward by one line and Up arrow
to scroll backward by one line.
Use the b
key to go back previous page. You can move up for a specific number of line by entering number followed by b
key. You also can search for a specific pattern using forward slash (/)
followed by pattern that you want to search. By pressing the Enter
key less will search forward for match cases. If you would like to search backwards use (?)
followed by pattern.
You will see (END)
at the end of the file. Press q
key to quite less and go back to the command line.
Less Options
if you want to see line number with less command, then use -N
option with less command:
$ less -N filename
When you exit from less, the content of given file will be cleared from the terminal screen. To keep content on the screen use -x
option with less command:
$ less -X filename
Use +F
option to view the file contents for changes:
$ less +F /var/log/file.log
Less Commands
Just type h with less command to get list of all commands to navigate through the file content and search for strings. Below are the some most common used commands:
Command | Description |
---|---|
Space bar or f | Move Forward one page. |
Down arrow, Enter, e, or j | Move forward one line. |
Up arrow,y or k | Move backward one line. |
b | Move Backward one page. |
/pattern | Search forward for matching patterns. |
?pattern | Search backward for matching patterns. |
N | Repeat previous search in reverse direction. |
n | Repeat previous search. |
g | Go to the first line in the file. |
Ng | Go to the N-th line in the file. |
G | Go to the last line in the file. |
p | Go to the beginning of fthe ile. |
Np | Go to N percent into file. |
h | Display help. |
q | Exit less. |
That’s all
If you face any error and issue in above steps , please use comment box below to report.
If our tutorials helped you, please consider buying us a coffee. We appreciate your support!
Thank you for your support.