Terminal GuideTerminal Guide

less Command Guide

less is a powerful pager for viewing files. Unlike cat, it allows you to navigate through large files efficiently.

6 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

less fileOpen file
cmd | lessPipe to less
less -N fileWith line numbers

Navigate

j/k or ↓/↑Line up/down
Space/bPage down/up
g/GStart/End of file

Search

/patternSearch forward
?patternSearch backward
n/NNext/prev match

Commands

qQuit
FFollow mode (tail)
vOpen in editor

Downloadable Image Preview

Failed to generate preview

Basic Usage

Open a file with less to view it page by page.

bash
less filename.txt

You can also pipe output to less:

bash
cat largefile.txt | less
ps aux | less

Basic Navigation

Space / Page DownMove forward one page
b / Page UpMove backward one page
j / Down ArrowMove forward one line
k / Up ArrowMove backward one line
gGo to beginning of file
GGo to end of file
qQuit less

Advanced Navigation

NgGo to line N (e.g., 100g)
NpGo to N percent of file
dMove forward half page
uMove backward half page
FFollow mode (like tail -f)

Searching

Search Commands

/patternSearch forward for pattern
?patternSearch backward for pattern
nGo to next match
NGo to previous match
&patternShow only lines matching pattern
bash
# Inside less, type:
/error     # Search for "error"
n          # Next match
N          # Previous match
Tip
Search patterns in less support regular expressions. Use /error|warning to find either.

Useful Options

Command Line Options

-NShow line numbers
-SChop long lines (no wrap)
-iCase-insensitive search
-ICase-insensitive search (even if pattern has uppercase)
-RDisplay ANSI color codes
-FQuit if content fits on one screen
-XDon't clear screen on exit
+FStart in follow mode
bash
# Show line numbers
less -N file.txt

# Preserve colors from command output
ls --color=always | less -R

# Don't wrap long lines
less -S wide_file.txt

# Start in follow mode (like tail -f)
less +F log.txt

Marks and Bookmarks

bash
# Set mark at current position
ma    # Set mark 'a'

# Return to mark
'a    # Go back to mark 'a'

Practical Examples

View log files with line numbers

bash
less -N /var/log/syslog

View colored output

bash
git diff | less -R
grep --color=always pattern file | less -R

View compressed files

bash
# less can read gzip files directly
less file.txt.gz

# Or use zless
zless file.txt.gz

Monitor log in follow mode

bash
less +F /var/log/syslog
# Press Ctrl+C to stop following, F to resume

View multiple files

bash
less file1.txt file2.txt
# Use :n for next file, :p for previous

Filter displayed lines

bash
# Inside less, show only lines with "ERROR"
&ERROR

less vs more

less is an improved version of more with additional features:

  • Backward navigation
  • Search functionality
  • Doesn't read entire file before displaying
  • Supports horizontal scrolling
Info
The name "less" is a play on the phrase "less is more".

Configure Default Options

Set the LESS environment variable for default options:

bash
# Add to ~/.bashrc or ~/.zshrc
export LESS="-R -i -N"

# -R: Show colors
# -i: Case-insensitive search
# -N: Show line numbers

Summary

less is the preferred way to view large files. Key takeaways:

  • Use Space/b to navigate pages
  • Use /pattern to search
  • Use -N for line numbers
  • Use -R to preserve colors
  • Use +F or F for follow mode
  • Press q to quit

Related Articles