>_cmd.script

less

Page through text interactively, one screen at a time

Files

By CMD Script Team · 4 min read · Last updated

SYNTAX
less [OPTIONS] [FILE...]

Options

Command options and flags
FlagDescription
-NShow line numbers in the left margin
-SChop long lines instead of wrapping them (enables horizontal scrolling)
-iIgnore case in searches unless the pattern contains uppercase letters
-FExit immediately if the entire file fits on one screen
-XLeave file contents on screen after less exits, instead of clearing it
+FStart in follow mode, similar to tail -f, showing new lines as they're appended

Distribution compatibility

  • Ubuntu
  • Debian
  • Fedora
  • Arch
  • macOS

What it does

less is a terminal pager: it displays a file (or piped input) one screen at a time and lets you navigate forward and backward, search for text, and jump around without loading the entire file into memory first. That last property makes it practical for huge files and live log-watching, where tools like cat are too blunt and more is too limited.

Beginner examples

  • less file.txt — open a file for interactive viewing
  • Press Space or PgDn to move forward a screen
  • Press b or PgUp to move backward a screen
  • Press q to quit
  • ps aux | less — page through the output of another command
less /etc/passwd

Advanced examples

  • Search forward for a pattern and cycle through matches: /error then n for next, N for previous
  • Jump straight to the end of the file: press G; jump back to the start with g
  • Follow a log file live, like tail -f, then stop following with Ctrl+C: less +F app.log
  • Show line numbers and disable line wrapping for wide files: less -N -S data.csv
  • Case-insensitive search regardless of the pattern's case: less -i then search
less +F /var/log/nginx/access.log

Common mistakes

  • Assuming less needs to load the whole file before showing anything — it doesn't, which is exactly why it's suited to huge files and live logs, unlike cat.
  • Forgetting q quits less and instead trying to Ctrl+C out of a large file view, which can behave unexpectedly if follow mode (F) is active.
  • Confusing less's backward search (?) with forward search (/) and wondering why matches appear "in the wrong direction."
  • Not realizing search is case-sensitive by default; add -i or toggle with -i inside less if you need case-insensitive matches.

Tips

  • Type a number before G (e.g. 50G) to jump directly to a specific line.
  • Use &pattern inside less to display only lines matching a pattern, similar to filtering with grep.
  • less respects your terminal's color codes, so piping colorized tool output (with --color=always type flags) through less -R preserves the colors.

Best practices

  • Prefer less over more for any interactive file viewing — it's strictly more capable and available on virtually every modern Linux and macOS system.
  • Use less +F instead of repeatedly running tail when actively debugging a live log, since you can drop out of follow mode to search history and resume without losing your place.
  • For scripts and non-interactive contexts, don't rely on less — it's built for interactive use; use cat, head, or sed instead when output needs to be captured or processed.

Try it yourself

A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.

Real-world use cases

  • Reviewing a multi-thousand-line application log without editing or loading it fully into an editor.
  • Watching a server's access log in real time while reproducing a bug, using less +F.
  • Searching a large configuration file for a specific setting with / before deciding whether to edit it.

Common interview questions

  • What's the difference between less and more? less can scroll both forward and backward and supports live search without reading the whole file up front; more is generally forward-only and simpler.
  • How would you watch a log file update in real time using less? less +F file.log enters follow mode, similar to tail -f; press Ctrl+C to stop following.
  • Why is less preferred over cat for large files? less doesn't need to load the entire file into memory before displaying it and lets you navigate interactively, whereas cat dumps the whole file to the terminal at once.

Frequently Asked Questions

How do I search inside a file while using less?

Press / followed by a pattern and Enter to search forward, or ? to search backward. Press n to jump to the next match and N for the previous one.

How do I quit less?

Press q. If you started less on multiple files, q moves to the next file unless you use -X or press capital Q / ZZ to quit entirely, depending on configuration.

What's the actual difference between less and more?

less can scroll backward (press b or use arrow keys/PgUp) and doesn't need to read the whole file before displaying it, while more can typically only page forward. less also supports live search and doesn't load the entire file into memory up front.

Can less follow a growing file like tail -f?

Yes. Run less +F file.log, or press Shift+F while already inside less, to enter follow mode. Press Ctrl+C to stop following and return to normal browsing.

Cheat sheet

Download a quick-reference cheat sheet for less.