How to Use Less Command in Linux: Syntax and Examples
Use the
less command in Linux to view the contents of a file one screen at a time. Run less filename to open the file, then use keyboard keys like Space to scroll down and q to quit.Syntax
The basic syntax of the less command is:
less [options] filename
Here, filename is the file you want to view. Options can modify behavior, like searching or line numbers.
bash
less filename.txt
Example
This example shows how to open a file named example.txt with less. You can scroll down with Space or Down Arrow, scroll up with b or Up Arrow, and quit with q.
bash
less example.txt
Output
Displays the content of example.txt one screen at a time until you press q to quit.
Common Pitfalls
Some common mistakes when using less include:
- Trying to edit the file inside
less(it is read-only). - Not knowing how to quit (press
qto exit). - Using
catfor large files, which floods the terminal instead of paging.
bash
cat largefile.txt
# floods terminal
less largefile.txt
# better for reading large filesQuick Reference
| Key | Action |
|---|---|
| Space | Scroll down one page |
| b | Scroll up one page |
| Enter | Scroll down one line |
| Up/Down Arrow | Scroll up/down one line |
| q | Quit less |
| /pattern | Search forward for pattern |
| ?pattern | Search backward for pattern |
| n | Repeat last search forward |
| N | Repeat last search backward |
Key Takeaways
Use
less filename to view files page by page in Linux.Press
Space to scroll down and q to quit less.less is read-only; it does not edit files.Use search inside
less with /pattern and navigate with n and N.Avoid using
cat for large files; less is better for readability.