less — Read large files safely
💬 "less command"
less reads and displays only as much as needed without loading the entire file into memory. You can open multi-GB logs.
cat outputs the entire file, so it is risky for large files. In practice, use less.
- Concept: a pager that lets you view a file screen by screen.
- Navigation: Space/PageDown next page, b previous page, g beginning, G end
- Search: /search-term next match, ?search-term previous match, n/N repeat
- Exit: press q to exit.
Commonly used options
| Option | Description |
|---|---|
| -N | Display line numbers |
| +F | Follow in real time like tail -f |
| -S | Scroll horizontally instead of wrapping long lines |
| +G | Jump to the end of the file as soon as it opens |
Three essential command combinations
less +F /var/log/nginx/access.log— Real-time log following (q to quit after stopping)less -N -S /etc/nginx/nginx.conf— Read configuration with line numbers and horizontal scrollingless +G /var/log/app.log— Start from the latest logs
💬 "Show me how to work with multiple files at once"
Here are common situations on production servers, organized step by step.
Step 1 · Real-time log following
Follow the logs and watch for anomalies.
less +F /var/log/nginx/access.log
Step 2 · Narrow down with search
Stop following with Ctrl+C and search with /.
/ 500
Step 3 · Inspect the relevant section
Look at the surrounding context to pinpoint the request that caused it.
n
In practice, use it like this
1. Tracing 500 errors
Situation — 5xx responses occur intermittently.
less +F /var/log/nginx/access.log
Result — Requests flow in real time, and after Ctrl+C you can use / 500 to view only those requests.
Key point — Follow → stop → search → inspect context is the basic flow of log analysis.
2. Skimming a large configuration file
Situation — Find a specific server block in an nginx configuration with thousands of lines.
less -N /etc/nginx/nginx.conf
Result — View line numbers and search with / server_name to find the block.
Key point — less is safe for read-only inspection without the risk of accidentally modifying the file with vi.
Things to keep in mind
- During real-time following (+F), editing keys do not work. Stop following with Ctrl+C.
- less is read-only, so you will not accidentally modify files (safer than vi).
- Outputting a multi-GB file with cat can freeze the terminal and increase server load.
Actual execution screen
This is the result of following logs in real time and checking 5xx responses.
!less command execution result: real-time log following
Live production server
Source (HTML): https://games.aidebatehub.com/programs/linux/filesystem/less.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments