less is a pager for viewing files. Unlike cat, it lets you scroll, search, and navigate large files efficiently.
Synopsis
less [OPTIONS] [FILE...]
Navigation
| Key | Action |
|---|
Space / f | Forward one screen |
b | Back one screen |
j / ↓ | Down one line |
k / ↑ | Up one line |
g | Go to beginning |
G | Go to end |
q | Quit |
Searching
| Key | Action |
|---|
/pattern | Search forward |
?pattern | Search backward |
n | Next match |
N | Previous match |
&pattern | Show only matching lines |
Common Options
| Option | Description |
|---|
-N | Show line numbers |
-S | Don’t wrap long lines |
-i | Case-insensitive search |
-R | Show ANSI colors |
+F | Follow mode (like tail -f) |
+/pattern | Start at first match |
+G | Start at end of file |
Examples
View a file
With line numbers
Follow log file (like tail -f)
$ less +F /var/log/syslog
# Press Ctrl+C to stop following, then navigate
# Press F to resume following
Start at pattern
$ less +/error /var/log/syslog
View command output
$ ps aux | less
$ dmesg | less
Preserve colors
$ ls --color=always | less -R
$ grep --color=always pattern file | less -R
In-Viewer Commands
| Command | Action |
|---|
h | Help |
v | Edit file in $EDITOR |
= | File info (line numbers, %) |
F | Follow mode (like tail -f) |
-N | Toggle line numbers |
-S | Toggle line wrap |
Mark positions
m + letter # Set mark (e.g., ma)
' + letter # Go to mark (e.g., 'a)
Tips
- “Less is more”: less has more features than more
- Set PAGER:
export PAGER=less for man pages etc. - LESSOPEN: Can be configured to view compressed files
- Use -R for colors: Essential when piping colored output
- Multiple files: Use
:n and :p to switch between files
Environment
# Add to ~/.bashrc
export LESS='-R -i -N' # Colors, case-insensitive, line numbers
export PAGER='less'
See Also
- more — Simpler pager
- cat — Output entire file
- head — First lines
- tail — Last lines
Tutorials