less Command in Linux: Complete Guide to Advanced File Paging, Navigation, and Parameters

less command in Linux and it perimeters

Somewhere around my second year of using Linux seriously, someone watched me open a huge log file with cat and scroll through hundreds of screens of terminal history to find something, and asked, not unkindly, why I wasn’t just using less. I didn’t have a good answer. Since that day, less has probably been the single command I run most often that isn’t cd, ls, or git. It’s the pager I reach for by default, and once you understand its search, navigation, and filtering features properly, it’s hard to imagine working in a terminal without it.

What less Does

less displays file content (or piped input) one screen at a time, but unlike its predecessor more, it supports full bidirectional scrolling, doesn’t need to read an entire file before it can start displaying it, and has a rich set of in-program commands for searching, navigating, and filtering content.

less [OPTIONS] FILE...

Basic usage:

less /var/log/syslog

Why “less Is more”: Key Advantages Over more

The name is a deliberate joke (“less is more”), but the practical advantages are real:

  • Backward scrolling works reliably and universally, not just in newer implementations.
  • Doesn’t require reading the whole file first — for a massive file, less can display the beginning immediately without waiting to read to the end, which matters enormously on multi-gigabyte log files.
  • Works on non-seekable input like pipes, still supporting most navigation features.
  • Richer search with regex support, forward and backward search, and highlighting.
  • Doesn’t clutter your scrollback buffer the way cating a huge file does — when you quit less, your terminal returns to exactly where it was, as though the file view never happened.

Interactive Navigation

Once inside less, these are the core interactive commands (standard, well-established less behavior):

Scrolling

  • Space or f — forward one screen.
  • b — backward one screen.
  • d — forward half a screen.
  • u — backward half a screen.
  • Enter or e or j — forward one line.
  • y or k — backward one line.
  • g — go to the first line of the file.
  • G — go to the last line of the file.
  • NUMBER g — go to a specific line number (e.g., 100g jumps to line 100).
  • p or NUMBER% — jump to a percentage through the file (e.g., 50% jumps to the halfway point).

Searching

  • /pattern — search forward for a regex pattern.
  • ?pattern — search backward for a regex pattern.
  • n — repeat the last search in the same direction.
  • N — repeat the last search in the opposite direction.
  • &pattern — display only lines matching the pattern (filtering).

Other Useful Commands

  • q — quit.
  • v — open the current file in your $EDITOR at the current line — genuinely useful when you spot something in a log that needs fixing in the source config and don’t want to reopen the file manually.
  • F — enter “follow mode,” behaving like tail -f, continuously showing new content appended to the file. Press Ctrl+C to stop following and return to normal browsing without losing your place.
  • m followed by a letter — set a mark at the current position; ' followed by that letter jumps back to it.
  • = or Ctrl+G — show the current file name, line number, and byte position.
  • h — display the full help screen listing every available command.

Core Command-Line Options and Parameters

-N — Show Line Numbers

less -N file.txt

Displays a line number to the left of each line, which I use constantly when I need to reference a specific line while debugging or discussing a file with a colleague.

-S — Chop (Don’t Wrap) Long Lines

less -S wide_data.csv

Instead of wrapping long lines onto multiple screen rows, -S truncates them to the terminal width, letting you scroll horizontally with the arrow keys. This is essential for viewing wide CSV files or logs with very long lines, where wrapping makes the output visually unreadable.

-i — Case-Insensitive Search (with a Smart-Case Twist)

less -i file.txt

Makes searches case-insensitive by default. less also has a related -I flag for always case-insensitive, and by default (without -i), search is case-sensitive only if the search pattern contains an uppercase letter — a “smart case” behavior similar to what many modern editors use.

-X — Don’t Clear the Screen on Exit

By default, less restores the terminal to its previous state when you quit, effectively “erasing” the pager view. -X disables this, leaving the last displayed screen visible in your scrollback after quitting — useful if you want the final view to remain readable after exiting.

-F — Quit Automatically if Content Fits on One Screen

less -F short_file.txt

If the file is short enough to fit entirely within the terminal window, less exits immediately instead of waiting for a keypress, behaving similarly to cat in that specific case, while still using pager behavior for longer files. This is commonly combined with -X in $LESS environment variable configuration.

-R — Interpret ANSI Color Escape Codes

grep --color=always "ERROR" app.log | less -R

Normally, less displays escape sequences (like those used for terminal colors) as raw, garbled control characters. -R tells it to interpret and correctly render ANSI color codes, which is exactly what you need when piping colorized output from tools like grep --color=always or git diff --color=always into less.

-M / -m — Verbose Prompt

Controls how much detail is shown in the status line at the bottom of the screen — -M shows detailed information (line numbers, percentage through file, file name), -m shows a more compact version.

+F — Start in Follow Mode

less +F /var/log/app.log

Immediately enters follow mode on startup, equivalent to tail -f but with the ability to press Ctrl+C and scroll back through history — this is genuinely one of the best reasons to prefer less +F over plain tail -f for log monitoring, since you retain full pager navigation the moment you stop following.

+NUMBER or +/pattern — Start at a Line or Search Match

less +100 file.txt
less +/ERROR app.log

Opens the file already positioned at line 100, or already scrolled to the first match of “ERROR” — saving a manual jump or search step after opening.

-n — Suppress Line Numbers Entirely

Useful for performance on extremely large files, since less normally needs to scan the file to know line numbers for jumps and status display; disabling this can speed up opening very large files where exact line-number tracking isn’t needed.

The $LESS Environment Variable

Rather than typing the same options every time, you can set default less behavior globally:

export LESS='-R -i -M -X'

I keep something close to this in my shell profile — -R so colorized output from grep/git/etc. always renders correctly, -i for convenient case-insensitive searching, and -M for a more informative status line.

Practical, Real-World Examples

1. Reading a Large Log File Efficiently

less /var/log/syslog

2. Viewing Colorized Diff or Grep Output

git diff --color=always | less -R

3. Following a Log Live, With the Ability to Scroll Back

less +F /var/log/nginx/error.log

Press Ctrl+C to stop following and investigate a specific section, then press F again to resume following from wherever the file currently is.

4. Viewing a Wide CSV Without Line Wrapping

less -S data.csv

5. Searching and Filtering a Massive Log for Specific Events

less /var/log/auth.log
# then inside less:
/Failed password
n
n

6. Comparing Man Pages Side by Side Using less’s Built-In Editor Jump

man bash
# press v to jump into $EDITOR at the current position for closer inspection or note-taking

less in Shell Scripting and Automation

Because less is fundamentally interactive, it’s rarely used inside unattended automation scripts the way sed or awk would be — but it’s extremely common as the value of the $PAGER environment variable, which git, man, psql, and many other tools invoke automatically:

export PAGER='less -R'

This ensures that any tool respecting $PAGER automatically gets colorized, search-capable output without needing per-tool configuration.

Comparing less to Related Commands

TaskBest Tool
Full-featured interactive pagingless
Simple forward-only paging on minimal systemsmore
Dumping a whole file with no interactioncat
Watching a growing log with scroll-back capabilityless +F
Piping colorized tool output for readable pagingless -R

The practical rule most Linux users follow: default to less whenever it’s available, since it’s a strict superset of what more offers, and reserve more for environments where less genuinely isn’t installed (some minimal containers, certain embedded or rescue environments).

Troubleshooting Common less Issues

Colors show up as garbled escape codes instead of actual colors — add -R to interpret ANSI color sequences correctly.

Long lines wrap awkwardly, breaking visual alignment of tabular data — use -S to chop long lines instead of wrapping, then scroll horizontally with arrow keys.

less seems to hang or is slow to open a huge file — this is often related to line-number tracking; try -n to suppress line numbering overhead, or check whether the file is on a slow network filesystem.

Search isn’t finding an obviously present string — check case sensitivity; by default, search is case-sensitive unless the pattern is all lowercase (smart case), or explicitly force case-insensitivity with -i.

less +F seems stuck and won’t show new lines — verify the file is actually being appended to (not truncated and replaced, which some log rotation setups do); for rotation-aware following behavior, tools like tail -F may be more appropriate, or ensure your less version and log rotation policy are compatible.

Performance Optimization

less is specifically designed to be efficient on large files because, for basic forward display, it doesn’t need to read the entire file into memory before showing content — it can begin displaying the beginning of a multi-gigabyte file essentially instantly. Where performance can degrade is with features requiring knowledge of the whole file, like jumping to a percentage (p) or displaying accurate total line counts, since those genuinely require scanning; on very large files, disabling such features with -n or being patient during the initial scan is the practical tradeoff.

Security Implications

As with any pager, be cautious about terminal escape sequence handling when viewing untrusted files — certain crafted escape sequences displayed through a pager have historically been used in terminal-based attacks (manipulating terminal state, or in rare cases exploiting terminal emulator vulnerabilities). less is generally considered one of the safer pagers in this regard due to its careful escape-sequence handling, but exercising the same caution you would with any tool displaying untrusted content is reasonable.

Compatibility Across Distributions

less is not part of a base POSIX toolset the way more technically is, but in practice it’s included by default on essentially every modern Linux distribution — Ubuntu, Debian, Fedora, RHEL/CentOS, Arch, openSUSE — and on macOS as well (as BSD-licensed less, maintained by the same long-running upstream project). Minimal container base images (like scratch-based or extremely stripped-down Alpine images) may omit it, in which case it needs to be explicitly installed (apt install less, apk add less, etc.) — this is genuinely one of the first packages worth adding back to any minimal image intended for interactive debugging use.

Summary

less is the pager to reach for by default on any modern Linux system — bidirectional scrolling, instant startup on huge files, regex search in both directions, ANSI color support via -R, and follow mode via +F/F cover essentially every practical need for reading, searching, and monitoring text content interactively. Setting up a sensible $LESS default (-R -i -M) and knowing the core navigation keys (/, n, g, G, q, F) will carry you through the overwhelming majority of situations where you need to look closely at a file from the terminal.

References

  • GNU less Manual: https://www.greenwoodsoftware.com/less/
  • less Official Documentation: https://greenwoodsoftware.com/less/man.html
  • man less (local manual page)
Total
0
Shares

Leave a Reply

Previous Post
fold command in Linux and it perimeters

fold Command in Linux: Complete Guide to Text Wrapping, Line Width Control, and Parameters

Next Post
lpr command in Linux and it perimeters

lpr Command in Linux: Complete Guide to Printing Files and Parameters

Related Posts