Using the less Text Viewer in Linux
Learn how to open, navigate, search, jump through, and exit text files with the Linux less terminal pager.
less is a Linux pager: a program that displays text one screen at a time and provides controls for moving through it. It is useful for reading files, command output, logs, and manual pages without printing the entire contents into the terminal at once.
This lesson assumes basic terminal use, including running commands from a shell prompt and understanding file names. For related background, see File Structure In Linux and Bourne Again Shell Bash.
What less Does
When a file is too large to fit on one terminal screen, displaying it with a command such as cat can cause the contents to scroll past quickly. less opens an interactive terminal viewer instead. You can read one screen, move forward or backward, search for text, and then return to the shell.
less is for viewing and navigating, not editing. Opening a file with less does not provide a way to change its contents. If you need to modify a file, use a terminal editor such as nano.
Opening a File in less
The general command format is:
less FILE
For example:
less sample_text
Replace FILE with the path to the text file you want to inspect. After you run the command, the file appears in a full-screen terminal interface. Usually, only the portion that fits on the current screen is visible at first.
less is useful for short files as well as long ones. It is especially helpful when a file or command output is larger than the terminal window.
Understanding the less Status Line
The bottom display area commonly contains a status line. The status line summarizes where you are in the file. Its exact formatting can vary, but it may show the file name, visible line range, total line count, and percentage position.
A line range identifies the first and last file lines currently visible on screen. The total line count represents the file's length in lines. The percentage position is an approximate indication of how far through the file you have moved. For example, a display near 50% means you are approximately halfway through the file.
| Displayed element | Meaning |
|---|---|
| File name | Identifies the file currently being viewed. |
| Visible line range | Shows which lines are currently on screen. |
| Total line count | Shows the file's total number of lines. |
| Percentage | Shows the approximate reading position in the file. |
Moving Through a File
Screen-by-screen navigation
While less is active, press Space or f to advance by one screen. Press b to move backward by one screen.
A screen movement changes the view by a page-sized amount. This is different from single-line movement, which changes the view only slightly and is useful for fine adjustments.
Line-by-line navigation
Use the Up Arrow key to move upward by one line and the Down Arrow key to move downward by one line. These keys are useful when screen movement goes slightly too far or when you want to examine neighboring lines carefully.
| Key or command | Action | Typical use |
|---|---|---|
| Space or f | Advance one screen | Continue reading forward |
| b | Return one screen | Review earlier content |
| Up Arrow | Move up one line | Fine adjustment upward |
| Down Arrow | Move down one line | Fine adjustment downward |
/search-text | Search forward for text | Locate a word or phrase |
line-number followed by g | Go to a specific line | Inspect a known line |
| q | Quit less | Return to the shell |
Searching Within a File
Searching is faster than manually scrolling through a large file. To start a forward search, press /. Type the desired search pattern, then press Enter.
/SEARCH_TERM
A search pattern is the text you enter after the search command. less moves to a matching location when it finds the pattern. For example, to look for the word error, press /, type error, and press Enter.
- Open the file with
less. - Press
/. - Type the word or phrase to find.
- Press
Enterto confirm the search. - Read the matching location and surrounding content.
Jumping to a Specific Line
When an error message or diagnostic identifies a line number, you can go there directly instead of scrolling. Type the line number, then press g.
120g
This example jumps to line 120. Direct line navigation is useful when inspecting a reported error, checking a configuration entry, or examining a known location in a source or log file. After jumping, review the surrounding lines with the arrow keys or page controls.
Exiting less
Press q to quit the pager.
q
Quitting returns you to the shell prompt. It does not modify the file you viewed. If you appear unable to type another shell command after opening a file, less is probably still active; press q.
Reading Manual Pages with less
The man command displays Linux manual pages. Its output is commonly sent through a pager such as less, so the same navigation keys apply when reading command documentation.
man less
This opens the manual page for less. Use Space or f to move forward, b to move backward, the arrow keys for line-by-line movement, / to search, and q to return to the shell.
less Compared with nano
Choose the tool based on your goal:
| Tool | Purpose | Choose it when |
|---|---|---|
less | Inspect and navigate through text | You only need to read, search, or review a file. |
nano | Edit text in a terminal | You need to add, remove, or change file contents. |
For example, use less to inspect a configuration or log file without intending to change it. Exit less with q, then use nano FILE when edits are required.
Practical Workflows
Read a sample text file
less sample_text
The file opens in the full-screen viewer. Read the visible portion, check the status line at the bottom, and use the navigation keys as needed.
Move through a long file
- Open the file with
less. - Press
Spaceorfto advance one screen. - Press
bto return one screen. - Use the
Up ArrowandDown Arrowkeys for single-line adjustments. - Press
qwhen finished.
Find a word or phrase
- Open the text file in less.
- Enter
/followed by the desired text. - Press
Enterto move to a matching location.
Inspect a reported line
- Open the relevant file with less.
- Type the target line number, such as
120g. - Review the target line and nearby content.
Troubleshooting
- Cannot return to the shell: The less pager is still active. Press
q. - Text is far down in a large file: Press
/, enter the search text, and pressEnter. If the line number is known, use the line-jump command instead. - Cannot edit the displayed file: less is a viewer, not an editor. Quit with
qand open the file withnanoif you need to modify it. - Numbers at the bottom are confusing: They belong to the status line and summarize the visible line range, total line count, and approximate position.
- Moved too far while reading: Use the
Up ArrowandDown Arrowkeys for one-line adjustments, or pressbto go back one screen.
Key Points
lessis a terminal pager for interactive, read-only text viewing.less FILEopens a file in a full-screen interface.Spaceandfmove forward one screen;bmoves backward one screen.- The arrow keys move one line at a time.
/SEARCH_TERMsearches forward for text.LINE_NUMBERfollowed bygjumps to a line.qexits less without changing the file.mancommonly uses less to display manual pages.- Use less for inspection and nano for editing.