less Command in Linux: Navigate and Search Files

By 

•

Updated on

•

7 min read

Linux Less Command

When you need to check a long log file or a configuration file on a server, opening it in a text editor is often overkill. The less command displays the contents of a file or a command output, one page at a time, and lets you navigate both forward and backward through the file.

Unlike text editors like vim or nano , less does not read the entire file on startup, which results in much faster load times, especially with large files . It is similar to more, but with more advanced features.

This guide explains how to use less to view, navigate, and search files, along with the options you will use most often.

How to Use Less

The general syntax for the less program is as follows:

txt
less [OPTIONS] filename

For example, to view the content of the /usr/share/common-licenses/GPL-3 file you would type:

Terminal
less /usr/share/common-licenses/GPL-3

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:

Terminal
ps aux | less

When you open a file whose content does not fit in one page, less shows the file name at the bottom of the screen. As you move through the file, the prompt changes to 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 f key.

You can press either the Down arrow or Enter to scroll forward by one line and Up arrow to scroll backward by one line.

To go back to the previous page, hit the b key. To move up for a specific number of lines, type the number followed by the b key.

To jump to the end of the file, press G. To return to the beginning, press g. If you only need the last lines, such as the newest entries in a log file, open the file at the end with the +G option:

Terminal
less +G filename

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.

Less Options

If you want less to show line numbers, launch the program with the -N option:

Terminal
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 -X option:

Terminal
less -X filename

The +F option tells less to watch the file contents for changes. This is useful when opening log files.

Terminal
less +F filename

When launched with +F, less behaves much like tail -f . To stop following and return to normal navigation, press Ctrl+C. You can also start following at any time by pressing F while viewing a file.

For a workflow that combines less +F with file, text, and systemd journal filters, see how to tail and filter logs in real time .

If the file contains long lines that wrap and make the output hard to read, use the -S option to chop lines at the screen edge instead of wrapping them:

Terminal
less -S filename

You can scroll horizontally with the Left arrow and Right arrow keys to see the cut-off parts.

By default, searches in less are case-sensitive. The -i option ignores case when the search pattern contains only lowercase letters. To ignore case in all searches, use -I:

Terminal
less -I filename

When the input contains ANSI color escape sequences, such as colored ls output or logs, the -R option displays the colors instead of the raw escape codes:

Terminal
ls --color=always /etc | less -R

Searching in Less

If you want to search for a pattern, type forward slash (/) followed by the pattern you want to search for. Once you hit Enter, less will search forward for matches and highlight them. To search backward, type a question mark (?) followed by the search pattern.

Press n to repeat the search in the same direction, or N to search in the opposite direction. After a backward search with ?, n continues backward and N searches forward. If the pattern does not appear in the file, less shows Pattern not found at the bottom of the screen.

The search pattern is a regular expression. For example, to find lines that contain either ERROR or WARN, type /ERROR|WARN and press Enter.

To change case sensitivity without restarting less, type -i while viewing the file. less displays Ignore case in searches at the bottom of the screen. Type -i again to switch back, and the message changes to Case is significant in searches.

The -i option ignores case only for patterns without uppercase letters. For an uppercase pattern such as ERROR|WARN, type -I instead to ignore case.

To show only the lines that match a pattern, type an ampersand (&) followed by the pattern. All other lines are hidden, and the prompt starts with & as a reminder that the view is filtered. To show all lines again, type & and press Enter without a pattern. This works much like grep , but you stay inside the file and can keep scrolling.

Matches stay highlighted after you move on. To turn the highlighting off, press Esc followed by u.

You can also open a file at the first match of a pattern with the -p option:

Terminal
less -p ERROR filename

Running less +/ERROR filename does the same thing.

Less Commands

The less program includes a number of commands that allow you to navigate through the file contents and search for strings. To view a full list of all commands, type h.

Most of the commands that you can enter from the keyboard are based on those used by both more and vi. The same action can be performed using different keys.

Below are some of the most frequently used commands to navigate through the file contents when viewed by less:

CommandAction
Down arrow, Enter, e, or jMove forward one line.
Up arrow, y, or kMove backward one line.
Space bar or fMove forward one page.
bMove backward one page.
/patternSearch forward for matching patterns.
?patternSearch backward for matching patterns.
nRepeat previous search.
NRepeat previous search in reverse direction.
gGo to the first line in the file.
NgGo to the Nth line in the file.
GGo to the last line in the file.
Np or N%Go to N percent into the file.
hDisplay help.
qExit less.

Viewing Multiple Files

You can open multiple files with less by specifying them as arguments:

Terminal
less file1.txt file2.txt file3.txt

Once opened, use the following commands to navigate between files:

  • :n - Go to the next file.
  • :p - Go to the previous file.
  • :e filename - Open a new file.

Marking Positions

When viewing large files, you can mark positions and return to them later. This is useful when you need to jump between different parts of a file.

To set a mark, press m followed by a lowercase or uppercase letter (e.g., ma to set mark a).

To return to a mark, press ' (single quote) followed by the letter (e.g., 'a to return to mark a).

To go back to where you were before the last jump, such as a search or G, type two single quotes ('').

Quick Reference

For a printable quick reference, see the less cheatsheet .

TaskCommand
View a fileless filename
View with line numbersless -N filename
Follow file changesless +F filename
Chop long linesless -S filename
Ignore case in all searchesless -I filename
Show ANSI colorsless -R filename
Open at end of fileless +G filename
Open at first matchless -p pattern filename
Search forward/pattern
Search backward?pattern
Next search resultn
Show only matching lines&pattern
Go to end of fileG
Go to start of fileg
Quitq

Conclusion

For a complete list of all options and commands, check the man less page. You may also want to look at related commands like cat , head , and tail .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page