Linux online course

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 elementMeaning
File nameIdentifies the file currently being viewed.
Visible line rangeShows which lines are currently on screen.
Total line countShows the file's total number of lines.
PercentageShows 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 commandActionTypical use
Space or fAdvance one screenContinue reading forward
bReturn one screenReview earlier content
Up ArrowMove up one lineFine adjustment upward
Down ArrowMove down one lineFine adjustment downward
/search-textSearch forward for textLocate a word or phrase
line-number followed by gGo to a specific lineInspect a known line
qQuit lessReturn 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.

  1. Open the file with less.
  2. Press /.
  3. Type the word or phrase to find.
  4. Press Enter to confirm the search.
  5. 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:

ToolPurposeChoose it when
lessInspect and navigate through textYou only need to read, search, or review a file.
nanoEdit text in a terminalYou 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

  1. Open the file with less.
  2. Press Space or f to advance one screen.
  3. Press b to return one screen.
  4. Use the Up Arrow and Down Arrow keys for single-line adjustments.
  5. Press q when finished.

Find a word or phrase

  1. Open the text file in less.
  2. Enter / followed by the desired text.
  3. Press Enter to move to a matching location.

Inspect a reported line

  1. Open the relevant file with less.
  2. Type the target line number, such as 120g.
  3. 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 press Enter. 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 q and open the file with nano if 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 Arrow and Down Arrow keys for one-line adjustments, or press b to go back one screen.

Key Points

  • less is a terminal pager for interactive, read-only text viewing.
  • less FILE opens a file in a full-screen interface.
  • Space and f move forward one screen; b moves backward one screen.
  • The arrow keys move one line at a time.
  • /SEARCH_TERM searches forward for text.
  • LINE_NUMBER followed by g jumps to a line.
  • q exits less without changing the file.
  • man commonly uses less to display manual pages.
  • Use less for inspection and nano for editing.