less Command
The less command is a terminal pager that displays file contents one screen at a time. It allows you to navigate through large files efficiently with features like backward scrolling, search functionality, and line numbering.
Syntax
Description
The less command is an improved version of the more command. It provides better navigation capabilities and doesn't exit automatically when reaching the end of a file.
Key features include:
- Forward and backward navigation through files
- Search functionality with pattern highlighting
- Line numbering and status information
- Multiple file handling
- Memory efficient for large files
Common Options
| Option | Description |
|---|---|
-N |
Show line numbers |
-S |
Chop long lines (don't wrap) |
-i |
Ignore case in searches |
-F |
Quit if entire file fits on one screen |
-X |
Don't clear screen on exit |
-R |
Display raw control characters (colors) |
+G |
Start at end of file |
+/pattern |
Start at first occurrence of pattern |
Navigation Keys
| Key | Action |
|---|---|
Space or f |
Forward one screen |
b |
Backward one screen |
Enter or j |
Forward one line |
k |
Backward one line |
g |
Go to beginning of file |
G |
Go to end of file |
/pattern |
Search forward for pattern |
?pattern |
Search backward for pattern |
n |
Next search match |
N |
Previous search match |
q |
Quit less |
h |
Help (show all commands) |
Examples
View a file
Opens the system log file for viewing
View file with line numbers
Displays the file with line numbers on the left
View multiple files
Opens multiple files (use :n for next file, :p for previous)
Start at end of file
Useful for viewing log files, starts at the most recent entries
Search for pattern from start
Opens file and jumps to first occurrence of "error"
View with colors preserved
Preserves color output when piping colored commands
Case-insensitive search
Makes all searches case-insensitive
Don't wrap long lines
Useful for viewing files with very long lines
Advanced Usage
Environment Variables
LESS- Default options for less commandLESSOPEN- Preprocessor for input filesPAGER- Default pager program
Common Environment Setup
Sets default options: case-insensitive search, raw colors, quit if one screen, don't clear screen
Viewing Compressed Files
View compressed files directly without extracting