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.
less [OPTION]... [FILE]...
command | less
Try it
A typical interactive session, shown read-only (our runner has no terminal and no less):
$ 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
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.
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
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'makeslesscolour-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 useless -R. less +Fversustail -f:less +Flets you press Ctrl+C to stop following, scroll and search, then pressFto follow again.tail -fcannot go back.- Compressed and special files: many systems install a
lesspipefilter, soless file.gzorless archive.tarshows decoded content. Enable it witheval "$(lesspipe)"where available. - Searching is a regular expression by default. Escape special characters, or press
Ctrl+Rbefore the pattern to search literally. - Marks and several files:
mplus a letter sets a mark,'plus the letter returns;:nand:pmove between files given on the command line. moreis the older, simpler pager with fewer keys. It exists everywhere, butlessis almost always available as well.- Not for scripts. Pagers wait for keystrokes, so never put
lessin a script or cron job. SetPAGER=cator usegit --no-pagerfor non-interactive runs. - Huge files:
lessloads on demand, soGon a large file may take a moment while it finds the end, but it does not use much memory.
Related
cat,head,tail,grep- Lessons: Viewing text, Getting help