less Command in Linux: Navigate and Search Files

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:
less [OPTIONS] filenameFor example, to view the content of the /usr/share/common-licenses/GPL-3 file you would type:
less /usr/share/common-licenses/GPL-3You 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 | lessNavigating Through the File Content
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:
less +G filenameWhen 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:
less -N filenameBy 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 filenameThe +F option tells less to watch the file contents for changes. This is useful when opening log files.
less +F filenameWhen 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:
less -S filenameYou 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:
less -I filenameWhen 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:
ls --color=always /etc | less -RSearching 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:
less -p ERROR filenameRunning 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:
Viewing Multiple Files
You can open multiple files with less by specifying them as arguments:
less file1.txt file2.txt file3.txtOnce 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 .
| Task | Command |
|---|---|
| View a file | less filename |
| View with line numbers | less -N filename |
| Follow file changes | less +F filename |
| Chop long lines | less -S filename |
| Ignore case in all searches | less -I filename |
| Show ANSI colors | less -R filename |
| Open at end of file | less +G filename |
| Open at first match | less -p pattern filename |
| Search forward | /pattern |
| Search backward | ?pattern |
| Next search result | n |
| Show only matching lines | &pattern |
| Go to end of file | G |
| Go to start of file | g |
| Quit | q |
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
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

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