less: view files page by page
less shows a file one screen at a time and lets you scroll and search in both directions. It is the default pager for manual pages and the right tool for reading logs. For short files, cat prints everything at once.
less [options] file ...
command | less
Examples
less /var/log/apt/history.log
less -N script.sh # with line numbers
less -S wide.csv # do not wrap long lines (scroll sideways with arrows)
less +G app.log # start at the end
less +F app.log # follow the file as it grows (like tail -f)
journalctl -b | less # page through command output
cat notes.txt # print a short file
cat part1 part2 > whole # join files
head -n 20 data.csv # first 20 lines (see tail for the end)
Keys inside less
| Key | Action |
|---|---|
| Space / b | Page down / up |
| ↓ ↑ or j k | One line down / up |
| g / G | Start / end of the file |
| /text | Search forward |
| ?text | Search backward |
| n / N | Next / previous match |
| &pattern | Show only lines matching the pattern |
| F | Follow new lines; Ctrl+C to stop following |
| -i | Toggle case-insensitive search |
| v | Open the file in your editor |
| h | Help |
| q | Quit |
Useful settings
Set default options in the LESS environment variable, for example in ~/.bashrc:
export LESS='-R -i -M' # keep colours, smart case search, detailed prompt
-R displays colour codes from commands such as grep --color=always or git log correctly. Compressed logs can be read with zless (see gzip).
cat tips
cat -nnumbers lines;cat -A(GNU) shows tabs as^Iand line ends as$, useful for finding stray spaces or Windows line endings.- You do not need
cat file | grep x:grep x filedoes the same with one process less. tac(GNU) prints a file with the last line first.
more is the older pager, still present on every Unix system. It only scrolls forward on some systems, which is where the name less ("less is more") came from.