What it does
less is a pager: it shows text page by page and lets you move up and down, search and follow a growing file. It reads only what it needs, so it opens huge log files at once. It never changes the file.
man, journalctl and git use less to show their output. The keys are the same everywhere.
How to write it
less [OPTIONS] [FILE...]
COMMAND | less [OPTIONS]Main parts
Keys inside less
Space,f,PgDn- One page down.
b,PgUp- One page up.
Enter,j,↓- One line down.
k,↑- One line up.
d/u- Half a page down / up.
g/G- First line / last line. 50g = line 50.
/pattern- Search forward (a regular expression).
?pattern- Search backward.
n/N- Next / previous match.
&pattern- Show only lines that match (filter). & alone = all lines again.
F- Follow the end of the file, like tail -f (Ctrl+C stops following).
mletter/'letter- Set a mark / go back to a mark.
-N(typedinless)- Turn line numbers on or off while reading.
:n/:p- Next / previous file (when you opened several).
v- Open the file in your editor ($VISUAL or $EDITOR).
h- Help with all keys.
q- Quit.
Options 40
Most used
Nothing matches this filter.
Examples 12
$ run as a normal user# run as root (or with sudo)
1Read a log file
less /var/log/messagesSpace = next page,
/= search,q= quit.2Start at the end of a file
less +G /var/log/secureThe newest lines are at the end of logs.
3Follow a log live
less +F /var/log/messagesLike
tail -f, but press Ctrl+C to stop following and search back, then F to follow again.4Read command output page by page
dnf list installed | lessAny long output becomes easy to read.
5Show line numbers
less -N /etc/ssh/sshd_configHelpful when an error message names a line.
6Long lines without wrapping
less -S /var/log/httpd/access_logEach log line stays on one screen line; use ← → to scroll.
7Keep colors from a command
grep --color=always -n error app.log | less -R-Rshows the color codes as colors.8Open at the first match of a word
less -p "Out of memory" /var/log/messagesJumps straight to the first OOM message.
9Search without caring about case
less -i /var/log/messages /failFinds fail, Fail and FAIL.
10Show only matching lines inside less
less /var/log/secure &Accepted&filters the view; type&and Enter to see all lines again.11Follow a log that is rotated
less --follow-name +F /var/log/myapp/app.logKeeps following the new file after logrotate.
12Quit at once when output is short
git log --oneline | less -FXShort output is printed and less ends; long output gets a pager.
Nothing matches this filter.
Watch out
- In follow mode (F), the keys do not work until you press Ctrl+C.
- less opens binary files too; if the screen looks broken, quit and use
filefirst.
Files
~/.lesshst- The history of searches.
~/.config/lesskey- Your own key bindings.
Tips to remember
- Space down, b up, / search, n next, G end, g start, F follow, q quit.
- "less is more": less can do everything more can, and scroll back.