Viewing files
less
Page through a file or command output one screen at a time, with search and scrolling in both directions.
less [OPTION...] [FILE...]
less is the pager: it shows a file one screen at a time and lets you scroll, search and jump
around without loading the whole thing into memory, so it opens a multi-gigabyte log instantly.
It is what man, git log and journalctl use to display their output, and the right tool
whenever something is too long for cat.
Everything interesting in less happens through keys, not options. The ones worth memorizing:
| Key | Action |
|---|---|
Space / b |
one screen forward / back |
j / k (or arrows) |
one line down / up |
g / G |
start / end of file |
/text / ?text |
search forward / backward |
n / N |
next / previous match |
&text |
show only lines matching text (& + Enter shows all again) |
F |
follow the file as it grows, like tail -f; Ctrl-C to stop |
-N (typed inside less) |
toggle line numbers |
-S (typed inside less) |
toggle chopping of long lines |
v |
open the file in $VISUAL / $EDITOR |
q |
quit |
A useful detail for scripts: when its output is not a terminal, less does not page at all; it
copies its input to its output like cat. The examples below run without a terminal, so they
show that behaviour; the paging itself only happens interactively.
Examples
Without a terminal, less behaves like cat
less todo.txt | head -n 3
buy milk fix login bug write tests
less sees that its output is a pipe, not a terminal, and simply copies the file through, so
head gets ordinary text. That is why aliasing a command to less does not break pipelines.
Page the output of another command
grep -n INFO logs/app.log | less
1:2026-03-14 08:00:01 INFO server started on port 8080 2:2026-03-14 08:00:02 INFO connected to database db01 5:2026-03-14 08:05:09 INFO user lookup id=7 not found 8:2026-03-14 08:16:50 INFO health check recovered 10:2026-03-14 08:25:00 INFO user id=2 updated by api 12:2026-03-14 08:30:05 INFO cleanup job removed 42 temp files
Piping into less is the everyday use: in a terminal you would get a scrollable,
searchable view of the matches. Here, without a terminal, the lines are passed straight
through.
Skip the pager when the text fits on one screen
less -F -X notes.txt
Meeting notes, March - Move backups to the new server - Rotate the API key before Friday - Check disk usage on web01 - TODO: write the deploy script Ask Priya about the error rate on web02.
-F makes less exit immediately if the whole file fits on one screen, and -X stops it
from clearing the screen on exit, so short output stays visible like cat. This pair is a
common setting for PAGER and LESS.
Set default options with the LESS variable
export LESS='-R -F -X -i'
less README.md | head -n 1
# demo
Options in $LESS apply to every less run, including those started by man or git.
-R passes color escape sequences through, -i makes searches case-insensitive unless you
type a capital letter. The file itself prints normally here.
Open a file at the first match
less +/ERROR logs/app.log
Not run in the sandbox: needs an interactive terminal; without one less copies the file and ignores the starting position. No output is shown because none was captured.
A + argument is run as a command when the file opens: +/ERROR searches for ERROR and
positions the view there, and +G opens at the end of the file. less +F file starts in
follow mode, a scrollable alternative to tail -f.
Long lines and line numbers for log reading
less -N -S access.log
Not run in the sandbox: needs an interactive terminal; without one less ignores display options like -N and -S. No output is shown because none was captured.
-N numbers the lines and -S chops lines at the screen edge instead of wrapping, so wide
log lines stay one per row; use the left and right arrow keys to scroll sideways.
Common pitfalls
Raw color codes show up as
ESC[31mjunk when you pipe colored output intoless. Useless -R, and ask the producer to keep its colors (grep --color=always,ls --color=always).lessis notmore:moregenerally cannot scroll back through piped input. On a minimal system withoutlessinstalled,moreis the fallback.Typing
-Ninsidelesstoggles an option for the running session; putting it on the command line or in$LESSsets it from the start. Both work, which surprises people who think options are command-line only.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
lessis the same program (by Mark Nudelman) on Linux and macOS; differences come from the version, not the platform.
Options worth knowing
A selection, described in our own words. For the full list, see less(1) or less --help.
| Option | What it does |
|---|---|
-F, --quit-if-one-screen | Exit at once if the whole input fits on one screen. |
-X, --no-init | Do not clear the screen on start and exit. |
-R, --RAW-CONTROL-CHARS | Show ANSI color sequences as colors instead of escaped text. |
-S, --chop-long-lines | Cut long lines at the screen edge instead of wrapping them. |
-N, --LINE-NUMBERS | Show a line number before each line. |
-i, --ignore-case | Case-insensitive search unless the pattern contains a capital letter. |
-I, --IGNORE-CASE | Case-insensitive search always. |
-n, --line-numbers | Do not track line numbers, which speeds up huge files. |
-M, --LONG-PROMPT | Show a verbose prompt with position and percentage. |
-J, --status-column | Show a column marking lines with search matches. |
Related
catConcatenate files and print them to standard output; also the quickest way to see a short file.headPrint the first lines (or bytes) of files; 10 lines by default.tailPrint the last lines of files, or follow a file as it grows with -f.grepPrint the lines of files (or standard input) that match a pattern.