less
100%

Command Line · Lesson 8

less

Learn how to navigate, search, and follow long text files interactively with less.

When a text file is too long for one screen, less lets you read it without sending the entire file scrolling through the terminal. Its name inspired the old Unix joke, "less is more," because more is another pager.

Opening a File

Start the pager by passing it a filename:

$ less /home/pete/Documents/text1

While less is active, keystrokes control the pager rather than starting ordinary shell commands. You return to the shell when you quit the pager.

Which command opens /var/log/syslog in an interactive pager?

Navigating in less

Use these keys while the pager is open:

  • Use Up, Down, Page Up, and Page Down to move by lines or screens.
  • Press g to jump to the beginning.
  • Press G to jump to the end.
  • Press u to move up half a screen or d to move down half a screen.
  • Press h to open the built-in help.

Which key jumps directly to the end of a file in less?

Searching in less

Type / followed by a pattern and press Enter to search forward. Begin with ? to search backward.

  • /search_term: Search forward for search_term.
  • ?search_term: Search backward for search_term.
  • n: Repeat the search in the same direction.
  • N: Repeat the search in the opposite direction.

After a forward search for error, which key repeats the search in the same direction?

Leaving less

Press q to quit less and return to the shell prompt.

Which key exits less and returns to the shell?

Starting less with Options

Options and initial commands can change how the pager starts:

$ less -N file.txt
$ less +G file.txt
$ less +F /var/log/syslog
  • -N: Show line numbers.
  • +G: Open at the end of the file.
  • +F: Follow new content as it is added, similar to tail -f.

While following a file with +F, press Ctrl+C to stop following and return to normal navigation, then press q to quit. Use -i for searches that ignore case unless the pattern contains an uppercase letter, or -I to ignore case regardless of the pattern.

Commands can also send output through a pipe to less:

$ dmesg | less

Which command opens /var/log/syslog and follows new content as it arrives?

To practice paging, searching, and reading system text, try these hands-on labs:

  1. Linux less Command: File Paging - Learn the Linux 'less' command for efficient text file viewing and navigation, including search, line numbers, and pattern matching.
  2. Viewing Log and Configuration Files in Linux - Learn essential Linux command-line skills for efficiently viewing and navigating text files, including system logs and configuration files, using commands like cat, more, and less.

Lesson complete

You finished less

You can now use less to inspect long files without flooding the terminal.

  • Open a file or piped command output in the pager.

  • Navigate to specific parts of the input.

  • Search forward or backward and repeat a search.

  • Show line numbers or follow growing content.

  • Quit safely and return to the shell.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Command Line