less – view file contents with paging

less command
less command

less is a pager for viewing files. Unlike cat, it lets you scroll, search, and navigate large files efficiently.

Synopsis

less [OPTIONS] [FILE...]
KeyAction
Space / fForward one screen
bBack one screen
j / Down one line
k / Up one line
gGo to beginning
GGo to end
qQuit

Searching

KeyAction
/patternSearch forward
?patternSearch backward
nNext match
NPrevious match
&patternShow only matching lines

Common Options

OptionDescription
-NShow line numbers
-SDon’t wrap long lines
-iCase-insensitive search
-RShow ANSI colors
+FFollow mode (like tail -f)
+/patternStart at first match
+GStart at end of file

Examples

View a file

$ less /var/log/syslog

With line numbers

$ less -N /etc/passwd

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

CommandAction
hHelp
vEdit file in $EDITOR
=File info (line numbers, %)
FFollow mode (like tail -f)
-NToggle line numbers
-SToggle 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