Where to start First steps track · 15 stages
Linux Labs

less — page through a long file with search

5 min Ubuntu 24.04 · less 590

The less pager shows a long file or command output screen by screen — scroll, search by word, follow the end.

Cheat sheet

6 commands Printable version
  • less /var/log/syslog open a file; q to quit
  • less -N app.conf with line numbers
  • less -S access.log don't wrap long lines
  • less +G app.log open at the end
  • less +F app.log follow the end, like tail -f
  • ps aux | less page through command output
  • inside: Space/b — screen forward/back, g/G — start/end,
  • /word — search, n/N — next/previous match, q — quit

Overview

less is a pager: a program that shows text one screen at a time and waits for keys. Unlike cat it doesn't dump everything at once, and unlike an editor it never changes the file.

The bottom line is a status line: the file name, a colon, or (END) at the end. You type commands right in the window, without Enter, except for searches:

Key What it does
Space, f / b a screen forward / back
↓ ↑, j / k a line forward / back
g / G to the start / end
/word Enter search forward
?word Enter search backward
n / N next / previous match
&word Enter show only lines with the word
F wait for new lines, Ctrl+C to stop
h help on keys
q quit

Other commands open less too: man, journalctl and systemctl status show long output through it. The same keys work there.

Options

7

Startup options can be toggled from inside as well: type -N in an open less, and line numbers appear. Full list — man less.

Option What it does Example
-N Show line numbers. less -N app.conf
-S Don't wrap long lines — scroll sideways with the arrows. less -S access.log
-i Ignore case in searches unless the pattern has capital letters. less -i app.log
-R Show colors instead of ESC[...m codes. grep --color=always err app.log | less -R
+F Open in follow mode at the end of the file, like tail -f. less +F app.log
+G Open at the last line. less +G app.log
-F Quit at once if the text fits on one screen. less -F short.txt

Nothing found. Try a shorter query.

Recipes

5

Find an error in a long log

bash
less /var/log/syslog
# inside: G — to the end, ?error Enter — the nearest error above,
# n — the next one above, N — back down

Search is case-sensitive. For case-insensitive search start with -i, or type -i inside less.

Keep only the lines you need on screen

Inside less, type &Failed and Enter: only lines containing Failed stay. & and Enter with no word bring back the whole text.

A table with long lines

bash
less -S /var/log/nginx/access.log   # lines are cut, → ← scroll sideways

Colored output

bash
grep --color=always error app.log | less -R   # -R keeps the colors

Open what you're reading in an editor

The v key opens the current file in the default editor — on Ubuntu that's usually nano. When you leave the editor, you're back in less.

Troubleshooting

5

How to quit less

q. If text is being typed at the bottom (a command or search has started), press Esc or Ctrl+C first, then q. To leave F mode press Ctrl+C, then q.

Pattern not found

The word isn't below the current position. / searches forward from the screen: go to the start (g) and repeat, or search backward with ?. Check the letter case.

"file" may be a binary file. See it anyway?

The file is binary, and reading it as text makes no sense. Answer n. file name tells what it is — see file.

ESC[31m and similar garbage instead of colors

The program printed color codes, and less shows them as characters. Run less -R.

Missing filename

less was started with no file and no pipe. Give a file (less app.log) or pipe output into it: command | less.

See also

  • cat — a short file in full, no paging.
  • head — only the start.
  • tail — only the end, and tail -f.
  • grep — find lines without paging through the file.
  • man-help — man pages open in the same less.
  • journalctl — the systemd journal, also through less.
Loading…