less Command Guide
less is a powerful pager for viewing files. Unlike cat, it allows you to navigate through large files efficiently.
6 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
less fileOpen filecmd | lessPipe to lessless -N fileWith line numbersNavigate
j/k or ↓/↑Line up/downSpace/bPage down/upg/GStart/End of fileSearch
/patternSearch forward?patternSearch backwardn/NNext/prev matchCommands
qQuitFFollow mode (tail)vOpen in editorDownloadable Image Preview
Failed to generate preview
Basic Usage
Open a file with less to view it page by page.
bash
less filename.txtYou can also pipe output to less:
bash
cat largefile.txt | less
ps aux | lessNavigation Commands
Basic Navigation
| Space / Page Down | Move forward one page |
| b / Page Up | Move backward one page |
| j / Down Arrow | Move forward one line |
| k / Up Arrow | Move backward one line |
| g | Go to beginning of file |
| G | Go to end of file |
| q | Quit less |
Advanced Navigation
| Ng | Go to line N (e.g., 100g) |
| Np | Go to N percent of file |
| d | Move forward half page |
| u | Move backward half page |
| F | Follow mode (like tail -f) |
Searching
Search Commands
| /pattern | Search forward for pattern |
| ?pattern | Search backward for pattern |
| n | Go to next match |
| N | Go to previous match |
| &pattern | Show only lines matching pattern |
bash
# Inside less, type:
/error # Search for "error"
n # Next match
N # Previous matchTip
Search patterns in less support regular expressions. Use
/error|warning to find either.Useful Options
Command Line Options
| -N | Show line numbers |
| -S | Chop long lines (no wrap) |
| -i | Case-insensitive search |
| -I | Case-insensitive search (even if pattern has uppercase) |
| -R | Display ANSI color codes |
| -F | Quit if content fits on one screen |
| -X | Don't clear screen on exit |
| +F | Start 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.txtMarks 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/syslogView colored output
bash
git diff | less -R
grep --color=always pattern file | less -RView compressed files
bash
# less can read gzip files directly
less file.txt.gz
# Or use zless
zless file.txt.gzMonitor log in follow mode
bash
less +F /var/log/syslog
# Press Ctrl+C to stop following, F to resumeView multiple files
bash
less file1.txt file2.txt
# Use :n for next file, :p for previousFilter displayed lines
bash
# Inside less, show only lines with "ERROR"
&ERRORless 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 numbersSummary
less is the preferred way to view large files. Key takeaways:
- Use
Space/bto navigate pages - Use
/patternto search - Use
-Nfor line numbers - Use
-Rto preserve colors - Use
+ForFfor follow mode - Press
qto quit