The less command in Linux is a powerful text file viewer that allows users to navigate through files efficiently without loading the entire file into memory. Unlike simpler commands like cat, less provides interactive navigation with forward and backward movement, search capabilities, and many other features.
It’s an improvement over the older more command (hence the name “less is more”) and is particularly useful when working with large log files or documentation.
Basic Usage Examples
Opening a File with Less
less filename.txt
This opens the file in the less pager, allowing you to scroll through the content.
Navigating Through Content
# Press Space or f - Forward one page
# Press b - Backward one page
# Press Down Arrow or Enter - Forward one line
# Press Up Arrow - Backward one line
# Press g - Go to the first line
# Press G - Go to the last line
# Press q - Quit less
These navigation commands make browsing through large files much more manageable.
Searching Within Files
# While in less, type:
/search_pattern # To search forward
?search_pattern # To search backward
n # To find the next occurrence
N # To find the previous occurrence
Searching is case-sensitive by default. This is extremely useful when looking for specific information in large log files.
Displaying Line Numbers
less -N filename.txt
This shows line numbers on the left side of the display, making it easier to reference specific parts of the file.
Opening Multiple Files
less file1.txt file2.txt file3.txt
Navigate between files using :n
(next file) and :p
(previous file).
Viewing Command Output with Less
command | less
Pipe the output of any command to less for easier navigation, for example:
ls -la | less
ps aux | less
find / -name "*.conf" 2>/dev/null | less
Marking and Returning to Positions
# While in less:
m followed by a letter # Mark current position with that letter
' followed by a letter # Return to the marked position
This is useful when navigating complex files and needing to return to specific sections.
Displaying Whitespace Characters
less -S filename.txt
This prevents line wrapping, showing long lines as they are and allowing horizontal scrolling.
Following File Changes (Like tail -f)
less +F logfile.txt
Similar to tail -f
, this follows additions to the file in real-time. Press Ctrl+C to stop following and return to normal less mode.
Custom Output Processing
less -r filename.txt # Display "raw" control characters (colors)
less -R logfile.txt # Display ANSI color escape sequences properly
Useful when viewing logs or outputs with color coding.
Viewing Multiple Files with Separate Windows
less -w file1.txt file2.txt
Displays multiple files in separate windows for side-by-side comparison.
Ignoring Case in Searches
# While in less, before searching:
-i
# Then perform your search
/search_pattern
This makes searches case-insensitive.
Advanced Commands and Customization
Executing Shell Commands from Less
# While in less, type:
!command
For example, !ls -la
executes the ls command and shows its output.
Opening a File at a Specific Line
less +100 filename.txt # Open file at line 100
This is helpful when you know exactly which part of the file you need to check.
Customizing Less with Environment Variables
# Add to your ~/.bashrc or ~/.zshrc:
export LESS="-R -i -g -c"
This sets default options for less (like case-insensitive searching) permanently.
Use Cases
- Examining large log files
- Reading documentation and man pages
- Reviewing code or configuration files
- Monitoring log file updates in real-time
- Viewing command output that would otherwise scroll off the screen
- Examining binary files safely (less will show you if the file is binary and ask before displaying it)
Key Shortcuts
- Space: Next page.
- b: Previous page.
- G: Jump to end of file.
- 1G: Jump to start.
- h: Display help menu.
The less
command exemplifies the Unix philosophy of creating simple tools that do one thing well. Its flexibility makes it an indispensable tool for system administrators, developers, and anyone who works with text files in a Linux environment.