LESS Command in Linux

Less is a command line utility that displays the contents of a file or a command output.

When starting less doesn’t read the entire file which results in much faster load times compared to text editors like vim or nano.

How to Start LESS

Format to start less:

less <logfilename>

Example:

less /var/log/syslog
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 Page Down, the f key, or Space bar.

To move down for a specific number of lines, type the number followed by the space or f key.

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 b key.

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.

To quit less and go back to the command line press q.

By default, when less exits, the file contents will be cleared from the screen. To leave file contents on screen, use the -X option:

less -X filename

The +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 +Fless will behave pretty much the same as tail -f.

LESS Commands

The 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 h.

Most frequently used commands to navigate through the file content when viewed by less:

Command Action
Down arrowEntere, or j Move forward one line.
Up arrow,y or k Move backward one line.
Space bar or f Move Forward one page.
b Move Backward one page.
/pattern Search forward for matching patterns.
?pattern Search backward for matching patterns.
n Repeat previous search.
N Repeat previous search in reverse direction.
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