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

less [OPTIONS] [FILE...]

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

less /var/log/syslog

Opens the system log file for viewing

View file with line numbers

less -N myfile.txt

Displays the file with line numbers on the left

View multiple files

less file1.txt file2.txt file3.txt

Opens multiple files (use :n for next file, :p for previous)

Start at end of file

less +G /var/log/messages

Useful for viewing log files, starts at the most recent entries

Search for pattern from start

less +/error logfile.txt

Opens file and jumps to first occurrence of "error"

View with colors preserved

ls --color=always | less -R

Preserves color output when piping colored commands

Case-insensitive search

less -i document.txt

Makes all searches case-insensitive

Don't wrap long lines

less -S wide-data.csv

Useful for viewing files with very long lines

Advanced Usage

Environment Variables

  • LESS - Default options for less command
  • LESSOPEN - Preprocessor for input files
  • PAGER - Default pager program

Common Environment Setup

export LESS="-iRFX" export LESSOPEN="| /usr/bin/lesspipe %s"

Sets default options: case-insensitive search, raw colors, quit if one screen, don't clear screen

Viewing Compressed Files

zless compressed.gz # or less compressed.gz # if LESSOPEN is configured

View compressed files directly without extracting

See also