Less Command in Linux
3 min read
Less is a command line utility that displays the contents of a file or a command output, one page at a time. It is similar to
more, but has more advanced features and allows you to navigate both forward and backward through the file.
less doesn't read the entire file which results in much faster load times compared to text editors like
less command is mostly used for opening large files.
How to Use Less
The general syntax for the
less program is as follows:
less [OPTIONS] filename
For example, to view the content of the
/usr/share/common-licenses/GPL-3 file you would type:
You can also redirect the output from a command to
less using a pipe. For example, to view the output of the
ps command page by page you would type:
ps aux | less
Navigating Through the File Content
When opening a file which content is too large to fit in one page, you will see a single colon (
To go forward to the next page press either the
f key or
Space bar. If you want to move down for a specific number of lines, type the number followed by the space or
You can press either the
Down arrow or
Enter to scroll forward by one line and
Up arrow scroll backward by one line.
To go back to the previous page hit the
b key. Move up for a specific number of lines, by typing the number followed by the
If you want to search for a pattern, type forward slash (
/) followed by the pattern you want to search. Once you hit
Enter less will search forward for matches. To search backwards use (
?) followed by the search pattern.
When the end of the file is reached, the string
(END) is shown at the bottom of the screen.
less and go back to the command line press
If you want
less to shows line numbers launch the program with the
less -N filename
By default, when
less exits, the file contents will be cleared from the screen. To leave file contents on screen, use the
less -X filename
+F option tells
less to watch the file contents for changes. This is useful when opening log files.
less +F /var/log/messages
When launched with
less will behave pretty much the same as
less program includes a number of commands that allows you to navigate through the file content and search for strings. To view a full list of all commands type
Most of the commands that you can enter from the keyboard are based on those used by both
vi. The same action can be performed using different keys.
Below are some of the most frequently used commands to navigate through the file content when viewed by less:
|Move forward one line.|
|Move backward one line.|
|Move Forward one page.|
|Move Backward one page.|
|Search forward for matching patterns.|
|Search backward for matching patterns.|
|Repeat previous search.|
|Repeat previous search in reverse direction.|
|Go to the first line in the file.|
|Go to the N-th line in the file.|
|Go to the last line in the file.|
|Go to the beginning of fthe ile.|
|Go to N percent into file.|
By now you should have a good understanding of how to use the
For a complete list of all options and commands type
man less in your terminal.