0
0
Linux-cliHow-ToBeginner · 3 min read

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 q to exit).
  • Using cat for large files, which floods the terminal instead of paging.
bash
cat largefile.txt
# floods terminal

less largefile.txt
# better for reading large files
📊

Quick Reference

KeyAction
SpaceScroll down one page
bScroll up one page
EnterScroll down one line
Up/Down ArrowScroll up/down one line
qQuit less
/patternSearch forward for pattern
?patternSearch backward for pattern
nRepeat last search forward
NRepeat 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.