less — Linux basic command
Linux / Basic Linux commands / less.html
An interactive viewer for navigating long files page by page. Supports reverse navigation.
Basic Information
| Command Name | less |
|---|---|
| One-line introduction | An interactive viewer for navigating long files page by page. Supports reverse navigation. |
| Provided by | GNU less (pre-installed on all Linux distributions) |
| Official documentation | https://man7.org/linux/man-pages/man1/less.1.html |
| Supported operating systems | Linux, macOS, BSD, WSL |
| Installation status | Pre-installed (no separate installation required) |
| Category | Linux basic command (tags: file viewer, page navigation, Linux, basic) |
What is this command?
less is an interactive viewer that allows you to view long files page by page. Unlike cat, it doesn't load the entire file into memory, only reading the necessary parts, so it can open very large files quickly.
It provides rich features such as forward/backward navigation, search, and moving to specific lines, and you can exit at any time with the q key.
What problem does it solve?
- Check long log files page by page.
- View very large files without memory overhead.
- Search for specific keywords within a file.
- Navigate backwards from the end of the file.
Main features
Page-by-page navigation
Navigate to the next page with the spacebar, and to the previous page with the b key.
- Usage scenario: When reading long files in order.
- Result obtained: You can read page by page without scrolling.
- Limitations: Key inputs may not be familiar to beginners.
Keyword search
Search with / key and move to the next result with n key.
- Usage scenario: When looking for specific errors or keywords in logs.
- Result obtained: You can quickly find the desired location.
- Limitations: Case sensitivity may apply, so caution is needed.
Reverse navigation
You can move upwards (to previous) with the b key or arrow keys.
- Usage scenario: When reading from the end of a file and need to go back upwards.
- Result obtained: Bidirectional navigation is possible.
- Limitations: Movement may be slow in very large files.
Real-time refresh (F key)
Pressing the F key tracks the end of the file in real-time, similar to tail -f.
- Usage scenario: When monitoring logs being recorded in real-time.
- Result obtained: When the file is updated, it is automatically displayed.
- Limitations: You need to press q to return to normal mode.
Installation and execution
Check pre-installation
Included by default in almost all Linux distributions.
which less
less --version
Installation (lightweight environment)
May only need installation in lightweight distributions.
sudo apt install less # Debian/Ubuntu
sudo dnf install less # Fedora
Actual usage examples
Basic file opening
View files page by page.
# Open file
less /var/log/syslog
# Basic operations:
# Spacebar: next page
# b: previous page
# /keyword: search
# q: exit
Keyword search
Search for specific content within a file.
# Open file
less /var/log/nginx/access.log
# Execute search
/error # Search for 'error'
# n: move to next result
# N: move to previous result
# Case-insensitive search
# less -i filename
Real-time log tracking
Monitor the end of the file in real-time.
# Open file
less /var/log/syslog
# Press F key for real-time tracking mode
# (same as tail -f)
# To return to normal mode, press Ctrl+C