Less Command in Linux command line

Less Command in Linux command line

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.


Navigating to the File Content

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:

CommandDescription
Space bar or fMove Forward one page.
Down arrow, Enter, e, or jMove forward one line.
Up arrow,y or kMove backward one line.
bMove Backward one page.
/patternSearch forward for matching patterns.
?patternSearch backward for matching patterns.
NRepeat previous search in reverse direction.
nRepeat previous search.
gGo to the first line in the file.
NgGo to the N-th line in the file.
GGo to the last line in the file.
pGo to the beginning of fthe ile.
NpGo to N percent into file.
hDisplay help.
qExit 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top