Read a Long File with less

Read a Long File with less

When the student list grows to hundreds of lines, printing it all at once sends most of it past the screen. less gives you a view you can move through instead. A program of this kind is called a pager: it shows a manageable part of a text file while you navigate.

You are still reading, not editing. Let's begin with a tiny class list so the controls are easy to try, then apply the same idea to a longer report. Knowing how to leave the pager is just as useful as knowing how to enter it.

What you’ll learn

  • Read a long report a screen at a time with less.
  • Search within the pager and exit without editing the report.

A pager shows a window onto a longer report

  1. Saved report

    The text file already exists before the pager starts.

    students.txt
  2. View

    less displays part of the file and lets you move or search.

    screen-sized window
  3. Exit

    q leaves the pager without saving edits to the report.

    file unchanged
Choose less when you want to move or search through text comfortably.

Open a prepared class list in less

Prepare the example

Run this in an interactive terminal with less installed. The first line creates the student list; the next line opens it. This short file may fit entirely on your screen, but you can still try searching and leaving the pager. Its display is interactive, so the output box describes behavior rather than an exact screen capture.

Bash · Linux terminal

printf 'Diya\nSaira\n' > students.txt
less students.txt

Expected behavior; machine-dependent details

Interactive view; press / to search and q to quit.

Once less students.txt starts, key presses go to the pager instead of becoming shell commands. Use Space to move forward through a longer file, or the pager's movement keys to change the view. Type / followed by a name such as Diya to search forward, then press Enter.

Press q when you want your shell prompt back. None of those reading actions saves an edited report. If the file fits on one screen, little movement may be visible; that is expected. This is why a pager is useful for understanding a large log or report without flooding the terminal with every line at once.

Prepare text and enter the pager

less opens text in a pager, allowing you to move through a file without printing the entire file at once. It is especially useful for logs, configuration files and command output.

What is less?

less is a terminal pager program used to view (but not edit) the contents of a text file one screen at a time. It's great for large files because it doesn't load the whole file into memory.


Step 1: Create a Sample File

Let's first create a simple file to test it on.

echo -e "Line 1\nLine 2\nLine 3\nLine 4\nLine 5" > sample.txt

This will create a file named sample.txt with a few lines.

Line 1
Line 2
Line 3
Line 4
Line 5

Step 2: View the File with less

Now let’s use less to view this file:

less sample.txt

When you run this command, the file opens in a paging interface. You won’t see the usual prompt — instead, you’re in a special viewer mode.


Move, search and leave the viewing session

Step 3: Navigating with less

Here are some helpful controls you can use while inside less:

  • Up/Down arrows: Scroll line by line
  • Spacebar: Scroll one full page down
  • b: Scroll one full page up
  • / followed by a word: Search forward for text
  • n: Repeat search
  • q: Quit and return to terminal

For example, to search for the word Line 3, type:

/Line 3

Then hit Enter and it will jump to the line with that text highlighted.


Step 4: Exit the Viewer

When you're done viewing the file, just press:

q

And you'll be back to your terminal prompt.


Navigate long files efficiently with less

  • less is ideal for viewing large files
  • It’s faster and more flexible than cat or more
  • It lets you scroll, search, and quit easily

Try it, then check the worked answer

Your task: Leave less without changing the report.

Run this in an interactive terminal with less installed. The first line creates the student list; the next line opens it. This short file may fit entirely on your screen, but you can still try searching and leaving the pager. Its display is interactive, so the output box describes behavior rather than an exact screen capture.

Bash · complete example for this question

printf 'Diya\nSaira\n' > students.txt
less students.txt

Expected behavior; machine-dependent details

Interactive view; press / to search and q to quit.

Worked answer

Press q.

Press q to leave less. Enter alone does not mean 'save this file'; it is a pager navigation key in this context. Check that your shell prompt returns, then open the original file again to see that reading it did not change its contents.

Check your decision

Leave less without changing the report.
Choose one answer. You can try again.

Answer and explanation

Press q.

Press q to leave less. Enter alone does not mean 'save this file'; it is a pager navigation key in this context. Check that your shell prompt returns, then open the original file again to see that reading it did not change its contents.

Use a pager when the report is longer than the screen

Choose less when you want to move or search through text comfortably. Recognize when the pager owns keyboard input, and leave it with q before typing the next shell command. Use an editor only when changing the file is actually your goal.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Page Through Text with more →