When you need to read the contents of a small text file in the Linux terminal, the cat command is perfectly sufficient. However, if you attempt to cat a 50-megabyte MySQL database dump or a dense system log, thousands of lines of text will instantly vomit onto your screen, flying by far too quickly to read. To read massive files efficiently, you must use a “pager” program. While older pagers like more exist, the modern standard is the less command. The primary advantage of less is that it does not attempt to load the entire file into RAM at once, meaning it can open a multi-gigabyte file almost instantly.
Opening and Navigating a File
Unlike cat, which dumps text and immediately returns your bash prompt, less creates an interactive, full-screen reading environment.
- Open your Linux terminal.
- Type the command followed by the filename:
less /var/log/syslog - Press Enter.
The terminal will clear, and the first page of the file will appear on your screen. You navigate this environment using keyboard shortcuts (many of which are identical to the classic vim text editor):
- Down Arrow or `j`: Move down exactly one line.
- Up Arrow or `k`: Move up exactly one line.
- Spacebar: Page down (scroll forward by one entire screen height).
- `b` key: Page up (scroll backward by one entire screen height).
- `G` (Capital G): Jump instantly to the absolute bottom of the file.
- `g` (Lowercase g): Jump instantly to the absolute top of the file.
- `q` key: Quit the
lessenvironment and return to your normal bash prompt.
Searching for Text Inside a File
If you are reading an Apache access log and want to find every instance where a user requested an image, you do not need to scroll through 10,000 lines manually. less includes a powerful, built-in search engine.
- While viewing a file inside
less, type a forward slash (/). - You will notice a prompt appear at the very bottom-left corner of the screen.
- Type your search term (e.g.,
/image.jpg). - Press Enter.
less will instantly jump to the first occurrence of that text and highlight it in bright white or yellow. Because the file likely contains multiple matches, you can navigate through the search results:
- Press the `n` key to jump forward to the next highlighted match.
- Press the `N` (Capital N) key to jump backward to the previous highlighted match.
Piping Output into Less
The less command is incredibly useful when combined with other noisy commands. If you run ls -lR /, the terminal will attempt to list every single file on your entire hard drive, overwhelming your screen. By piping the output of that command directly into less, you can view the results one page at a time.
ls -lR / | less