less

Overview

The less command is a file pager that allows forward and backward movement in a file. It’s more feature-rich than more and doesn’t need to read the entire file before starting.

Syntax

less [options] file...

Common Options

Option Description
-N Show line numbers
-i Case-insensitive search
-g Highlight only last match
-s Squeeze multiple blank lines
-F Quit if entire file fits on screen
-X Don’t clear screen on exit
-R Output “raw” control characters
-S Chop long lines
+F Follow mode (like tail -f)

Key Use Cases

  1. View large files
  2. Search through files
  3. Monitor log files
  4. Read documentation
  5. Text navigation

Examples with Explanations

Example 1: Basic Usage

less file.txt

View file with pagination

Example 2: With Line Numbers

less -N file.txt

Show line numbers while viewing

Example 3: Follow Mode

less +F logfile

Monitor file updates in real-time

Understanding Output

Navigation Commands: - Space/f: Forward one window - b: Backward one window - g: Go to start - G: Go to end - /pattern: Search forward - ?pattern: Search backward - n: Next match - N: Previous match - q: Quit

Common Usage Patterns

  1. View with line numbers:

    less -N file
  2. Case-insensitive search:

    less -i file
  3. Monitor logs:

    less +F /var/log/syslog

Performance Analysis

  • Memory efficient
  • Handles large files well
  • Quick startup time
  • Search optimization
  • Screen buffer management

Additional Resources

Advanced Features

  1. Multiple file handling
  2. Bookmarks
  3. Shell command execution
  4. Pattern highlighting
  5. Line filtering

Key Bindings

Key Action
h Help
q Quit
f Forward one window
b Backward one window
g First line
G Last line
/pattern Search forward
?pattern Search backward
n Next search match
N Previous search match
v Edit current file