Linux less Command


The less command in Linux is used for viewing file contents in a paginated way, which is particularly helpful for large files that don't fit on a single screen. Unlike cat, less lets you scroll up and down through a file, search for text, and view portions of the content without loading the entire file into memory.

Basic Syntax

less [options] file

Key Features of less

  • Scrolling: View content page-by-page or line-by-line.
  • Searching: Find specific text patterns within the file.
  • Navigation: Move through the file using keyboard commands.

Example with Output

Example 1: Viewing a File

Using less to view a file opens it in a paginated format, allowing you to scroll through the content.

Command
less file.txt
Output (on-screen navigation)
This is the content of file.txt. It spans multiple lines and pages. ---- (END)

Explanation:

  • When you open file.txt with less, only a portion of the file appears on the screen.
  • You can scroll down with Enter (line-by-line), Space (page-by-page), or scroll up with b.
  • Press q to quit and return to the command prompt.

Example 2: Searching for Text in a File

Within less, you can search for specific text patterns using / followed by the search term.

Command
less file.txt

Then, within less, type:

/word
Output
This is the content of file.txt. Here is the searched word in the middle of the content. ----

Explanation:

  • /word searches for the first instance of "word" in file.txt.
  • Press n to move to the next occurrence and N to move to the previous one.

Example 3: Displaying Line Numbers

The -N option displays line numbers in the less viewer, making it easier to reference specific lines in the file.

Command
less -N file.txt
Output
1 This is the content of file.txt. 2 Here is some additional text. 3 This is the last line. ---- (END)

Explanation:

  • less -N file.txt shows line numbers on the left side of each line.
  • Line numbers help identify specific sections, especially in large files.

Example 4: Viewing Output from Another Command

You can pipe the output of other commands (e.g., ls -l) to less to view long command output more easily.

Command
ls -l | less
Output
total 16 -rw-r--r-- 1 user user 1234 Oct 29 10:00 file1.txt -rw-r--r-- 1 user user 5678 Oct 29 10:10 file2.txt ---- (END)

Explanation:

  • ls -l | less shows the output of the ls -l command one page at a time.
  • This is useful for commands with long output, so you don’t miss any details.

Navigation Commands Within less

Once in less, you can use various keyboard commands for navigation:

CommandDescription
SpaceMove forward one page.
EnterMove forward one line.
bMove back one page.
/textSearch for "text" forward.
nRepeat the last search forward.
NRepeat the last search backward.
gGo to the beginning of the file.
GGo to the end of the file.
qQuit less and return to the shell.

Summary of Common less Commands

  • less file.txt: View the file with scrolling capabilities.
  • less -N file.txt: View the file with line numbers.
  • ls -l | less: View the output of ls -l with pagination.
  • Within less, use /search_term: Search for text patterns in the file.

The less command is an efficient, user-friendly way to navigate large files in Linux, providing fast scrolling, text searching, and various navigational tools without modifying the file content.