The less command is a powerful file viewer in Linux that allows you to navigate through files efficiently. Unlike cat, it provides forward and backward navigation, search capabilities, and doesn’t load the entire file into memory.
Basic Syntax
less [options] file(s)
Common Options
-N: Show line numbers-S: Chop long lines (don’t wrap)-i: Case-insensitive search-M: Show detailed prompt-F: Quit if entire file fits on first screen-X: Don’t clear screen on exit-R: Output “raw” control characters (for colored text)
Navigation Commands
Basic Movement
Spaceorf: Forward one windowb: Backward one windowd: Forward half windowu: Backward half windowjor↓: Forward one linekor↑: Backward one lineG: End of fileg: Beginning of fileq: Quit less
Search Navigation
/pattern: Search forward?pattern: Search backwardn: Next occurrenceN: Previous occurrence&pattern: Display only matching lines
Real-World Examples
1. Basic File Viewing
# View file with line numbers
$ less -N large_file.txt
# View multiple files
$ less file1.txt file2.txt
# View with raw control characters (colors)
$ less -R colored_output.log
2. Log File Analysis
# View log file from end
$ less +G /var/log/syslog
# Follow log updates
$ less +F /var/log/apache2/access.log
# Search in logs
$ less +/error /var/log/syslog
3. Source Code Reading
# View with line numbers and no wrap
$ less -NS source_code.cpp
# View multiple files
$ less *.py
Common Use Cases
-
Log Analysis
# View large log files less /var/log/syslog # Monitor log in real-time less +F /var/log/apache2/error.log -
Code Review
# View source code less -N source.cpp # Compare files (use :n and :p) less file1.cpp file2.cpp -
Documentation Reading
# View man pages man less | less # Read formatted text less README.md
Tips and Tricks
-
Pattern Matching
# Highlight search matches less -p"pattern" file.txt # Case-insensitive search less -i file.txt -
Line Numbers and Marks
# Show line numbers less -N file.txt # Set mark at current position (m + letter) ma # Jump to mark (' + letter) 'a -
Multiple File Handling
# Navigate between files :n # next file :p # previous file
Best Practices
-
Use Appropriate Options
# For logs with color less -R logfile.log # For code review less -NMS sourcecode.cpp -
Efficient Navigation
# Jump to percentage 50p # Go to 50% of file # Jump to line :100 # Go to line 100 -
Search Effectively
# Search and highlight less -p"ERROR" log.txt # Filter view &error # Show only lines with "error"
Common Errors and Solutions
-
Binary File Display
# View binary safely less -f binary_file # Alternative: use hexdump hexdump -C binary_file | less -
Large File Handling
# Memory efficient viewing less +F large_file.log # Use with tail tail -f large_file.log | less +F -
Character Encoding Issues
# Handle UTF-8 LESSCHARSET=utf-8 less file.txt
Related Commands
more: Simple pagercat: Concatenate filestail: View end of filehead: View beginning of filevim: Text editor with viewing capabilities
Advanced Usage
1. Custom Prompt
# Show detailed status line
export LESS='-M'
# Custom prompt format
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
2. Preprocessing
# View compressed files
zless compressed.gz
# View formatted output
json_pp < data.json | less -R
3. Advanced Filtering
# Multiple pattern matching
&pattern1|pattern2
# Exclude pattern
&!pattern
Keyboard Shortcuts Reference
Navigation
h Help
q Quit
f Forward one window
b Backward one window
d Forward half window
u Backward half window
j or ↓ Forward one line
k or ↑ Backward one line
G End of file
g Beginning of file
Searching
/pattern Search forward
?pattern Search backward
n Next occurrence
N Previous occurrence
&pattern Show only matching lines
Marks
ma Mark current position as 'a'
'a Go to mark 'a'
'' Return to previous position
Remember that less is more than just a file viewer – it’s a powerful tool for navigating and analyzing text files. Its ability to handle large files efficiently and provide advanced search capabilities makes it indispensable for system administration and development tasks.