Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Viewing and Searching Files

Linux less command

Reference for the Linux less pager: scroll, search with / and ?, jump with g and G, follow growing files with F, and use options like -S, -N and -R.

less is a pager: it shows a file or a command's output one screen at a time and lets you scroll in both directions, search, and jump around. Unlike cat, it does not dump everything at once, and unlike an editor it never changes the file. It also does not read the whole file first, so it opens a multi-gigabyte log immediately.

The name is a joke on its predecessor more, which could only move forward. less is the default pager for man, git log and many other tools. It is interactive, so our runner cannot demonstrate it; the key table below is the part to remember, and the runnable examples show the non-interactive pieces around it.

Terminal
less [OPTION]... [FILE]...
command | less

Try it

A typical interactive session, shown read-only (our runner has no terminal and no less):

Terminal
$ less /var/log/syslog
# G        jump to the end
# /error   search forward for "error", n = next match, N = previous
# &error   show only lines that match
# F        follow new lines (Ctrl+C to stop following)
# q        quit
Bash What less does when output is not a terminal (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Preparing output for a pager

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
less [-cEeFfgiMmNnqRrSsXx] [+COMMAND] [FILE]...

Common options:

Option Description
-N Show line numbers.
-S Chop long lines instead of wrapping (scroll sideways with the arrow keys).
-R Pass ANSI colour sequences through, so coloured output stays readable.
-i / -I Case-insensitive search (unless the pattern has capitals / always).
-F Quit at once if the whole input fits on one screen.
-X Do not clear the screen on exit. Often used as -FRX.
-M Long prompt with file position and percentage.
+G, +/text Start at the end / at the first match.
+F Start in follow mode, like tail -f.

Keys inside less:

Key Action
Space, f, Page Down Forward one screen
b, Page Up Back one screen
d, u Half a screen down / up
Enter, j, Down Forward one line
k, Up Back one line
g, G Start / end of the file (50g jumps to line 50)
/pattern, ?pattern Search forward / backward (regular expressions)
n, N Next / previous match
&pattern Show only matching lines (empty pattern to clear)
F Follow the end of the file (Ctrl+C to leave)
v Open the file in $EDITOR
h Built-in help
q Quit

Returns: 0 on normal exit; non-zero if a file cannot be opened.

Usage notes

  • Set defaults with LESS: export LESS='-FRXi' makes less colour-aware, case-insensitive and quiet for short output. Git sets similar defaults for its pager.
  • Pipes work: ps aux | less, grep -r todo . | less. If a command shows colour only on a terminal, add its colour-forcing option (--color=always) and use less -R.
  • less +F versus tail -f: less +F lets you press Ctrl+C to stop following, scroll and search, then press F to follow again. tail -f cannot go back.
  • Compressed and special files: many systems install a lesspipe filter, so less file.gz or less archive.tar shows decoded content. Enable it with eval "$(lesspipe)" where available.
  • Searching is a regular expression by default. Escape special characters, or press Ctrl+R before the pattern to search literally.
  • Marks and several files: m plus a letter sets a mark, ' plus the letter returns; :n and :p move between files given on the command line.
  • more is the older, simpler pager with fewer keys. It exists everywhere, but less is almost always available as well.
  • Not for scripts. Pagers wait for keystrokes, so never put less in a script or cron job. Set PAGER=cat or use git --no-pager for non-interactive runs.
  • Huge files: less loads on demand, so G on a large file may take a moment while it finds the end, but it does not use much memory.