Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

less

Read a file or command output one screen at a time, with scrolling and searching.

Level
RHCSA exam
Group
Text and search
Needs root
No
Package
less

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 (typed in less)
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

-N, --LINE-NUMBERS
Show line numbers.
-i, --ignore-case
Searches ignore case, unless the pattern has capitals.
-I, --IGNORE-CASE
Searches always ignore case.
+F
Start in follow mode (like tail -f).
+cmd
Run a less command at the start, like +G (go to the end) or +/error.
-S, --chop-long-lines
Do not wrap long lines; scroll sideways instead.
-R, --RAW-CONTROL-CHARS
Show colors (ANSI codes) correctly.
-r, --raw-control-chars
Show all control characters raw.
-F, --quit-if-one-screen
Quit at once when the text fits on one screen.
-X, --no-init
Do not clear the screen when you quit.
-p, --pattern=pattern
Start at the first match of the pattern.
-s, --squeeze-blank-lines
Show many empty lines as one.
-x, --tabs=n
Tab stops every N columns.
-G, --HILITE-SEARCH
Do not highlight matches.
-J, --status-column
Show a column that marks lines with matches.
-m, --long-prompt / -M, --LONG-PROMPT
Show more info in the prompt (percent, lines).
-n, --line-numbers
Do not count line numbers (faster on huge files).
-e, --quit-at-eof / -E, --QUIT-AT-EOF
Quit at the second / first time you reach the end.
-c, --clear-screen
Repaint from the top instead of scrolling.
-a, --search-skip-screen
Start searching after the visible screen.
-j, --jump-target=n
Show matches on line N of the screen.
-z, --window=n
Scroll N lines per page.
-h, --max-back-scroll=n, -y, --max-forw-scroll=n
Limit scrolling.
-b, --buffers=n, -B, --auto-buffers
Control the memory buffers.
-o, --log-file=file
Copy the input to a file (when reading a pipe).
-k, --lesskey-file=file
Use a key binding file.
-L, --no-lessopen
Ignore the LESSOPEN preprocessor.
-K, --quit-on-intr
Quit when you press Ctrl+C.
-q, --quiet
No bell sound.
-w, --hilite-unread
Highlight the first new line after a page move.
-u, --underline-special
Treat backspaces and carriage returns as normal characters.
-~, --tilde
Do not show ~ after the end of the file.
-#, --shift=n
Scroll N columns sideways with ← →.
--mouse
Use the mouse wheel to scroll.
--incsearch
Search while you type the pattern.
--use-color
Use colors in the less interface.
--follow-name
In follow mode, reopen the file when it is replaced (log rotation).
-V, --version
Show the version.
-?, --help
Show help.

Examples 12

$ run as a normal user# run as root (or with sudo)

  1. 1Read a log file

    less /var/log/messages

    Space = next page, / = search, q = quit.

  2. 2Start at the end of a file

    less +G /var/log/secure

    The newest lines are at the end of logs.

  3. 3Follow a log live

    less +F /var/log/messages

    Like tail -f, but press Ctrl+C to stop following and search back, then F to follow again.

  4. 4Read command output page by page

    dnf list installed | less

    Any long output becomes easy to read.

  5. 5Show line numbers

    less -N /etc/ssh/sshd_config

    Helpful when an error message names a line.

  6. 6Long lines without wrapping

    less -S /var/log/httpd/access_log

    Each log line stays on one screen line; use ← → to scroll.

  7. 7Keep colors from a command

    grep --color=always -n error app.log | less -R

    -R shows the color codes as colors.

  8. 8Open at the first match of a word

    less -p "Out of memory" /var/log/messages

    Jumps straight to the first OOM message.

  9. 9Search without caring about case

    less -i /var/log/messages
    /fail

    Finds fail, Fail and FAIL.

  10. 10Show only matching lines inside less

    less /var/log/secure
    &Accepted

    & filters the view; type & and Enter to see all lines again.

  11. 11Follow a log that is rotated

    less --follow-name +F /var/log/myapp/app.log

    Keeps following the new file after logrotate.

  12. 12Quit at once when output is short

    git log --oneline | less -FX

    Short output is printed and less ends; long output gets a pager.

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 file first.

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.