How to Use the less Command in Linux with Examples

March 23, 2022

Introduction

The less command is a Linux terminal pager that shows a file's contents one screen at a time. It is useful when dealing with a large text file because it doesn't load the entire file but accesses it page by page, resulting in fast loading speeds.

less is equipped with interactive features allowing users to navigate forward and backward through the file. The less tool is more advanced and versatile than other terminal pagers, such as more and most.

In this tutorial, you will learn to use the less command in Linux.

Learn to use the Linux less command.

Prerequisites

  • A system running Linux.
  • Access to a terminal (Ctrl + Alt + T).

Less Command Syntax

The general syntax for the less command is:

less [options] file_path

The [options] customize the less command output. Running the command without options outputs the input file's contents in the default manner.

The following section lists the most common less command options.

Less Command Options

Without additional options, less displays the output in the default format and assumes default behavior. The options modify the output or change how less acts while processing the file.

The most used less command options are:

OptionDescription
-Eless automatically exits upon reaching the end of file.
-fForces less to open non-regular files (a directory or a device-special file).
-FExit less if the entire file can be displayed on the first screen.
-gHighlights the string last found using search. By default, less highlights all strings matching the last search command.
-GRemoves all highlights from strings found using search.
-iIgnores case sensitivity during search.
-JDisplays a status column on the left side of the screen. The status column shows the lines that matched the current search and any lines marked using the m or M command.
-mInstructs less to prompt verbosely (similar to more), showing the percentage into the file. By default, less prompts with a colon.
-MInstructs less to prompt even more verbosely than more.
-nRemoves line numbers from the screen.
-NDisplays line numbers at the beginning of each line.
-o[file_name]Causes less to copy its input to the specified file. This option applies only when the input file is a pipe (|), not an ordinary file. For existing files, less asks for confirmation before overwriting the file.
-p[pattern]Instruct less to start at the first occurrence of the specified pattern in the input file.
-QEnforces quiet operation that silences the terminal bell.
-sMerges consecutive blank lines into a single blank line.
-XDisable clearing the screen after quitting less.
-z[n]Changes the default scrolling window size to the specified n lines.

For a complete list of options, refer to the less help file by running:

less --help
Less command options in the help file.

Navigating in the Text File

The less command accepts keyboard shortcuts that facilitate text navigation, especially when reading large files. The following table comprises the most commonly used shortcuts:

ShortcutsAction
Down Arrow, Enter, e, jOne line forward.
Up Arrow, y, kOne line backward.
Space bar, Page DownOne page forward.
Page Up, bOne page backward.
Right ArrowScroll right.
Left ArrowScroll left.
Home, gJump to the beginning of the file.
End, GJump to the end of the file.
/[string]Search forward for the specified string.
?[string]Search backward for the specified string.
nNext match during a search.
NPrevious match during a search.
qQuit less.

For a detailed list of navigation options, run:

less --help
Navigation options in the less command help file.

Note: To easily manage files with many lines, you can use Linux split command.

Less Command Examples

Below are examples of common use cases for the less command.

1. Open a Text File

Load a text file into less by specifying the file path.

For example:

less /etc/updatedb.conf

The configuration file loads, and the opening lines of the file are displayed in the terminal. The bottom-left corner of the display shows the file name and path.

Open a text file in less.

Use the navigation shortcuts to move forward, backward, or search for specific strings in the file.

2. Show Line Numbers

Use the -N option to display the specified text file with line numbers. Displaying line numbers is useful for code reviews or paired programming because they make it easier to locate a specific issue.

For example:

less -N /etc/init/mysql.conf
Show line numbers in a text file.

The file opens, and each line in the file is numbered.

3. Search for a String

The less pager allows you to search for a string in an open file. Initiate a forward search by pressing / and typing the search phrase. The search phrase is displayed at the bottom line of the display.

Note: By default, searching in less is case-sensitive. Ignore case sensitivity by specifying the -I option or pressing the I key within less.

Press Enter to confirm the search phrase and see the results. The display moves to the first page containing the search phrase and highlights the item. Move to the next item by pressing n, or see the previous one by pressing N.

For example:

Initiate a search in less.

Initiate a backward search from the current position in the file by pressing ? and typing the search phrase. When performing a backward search, the n and N key search directions are reversed as well - n finds the next item towards the beginning of the file, while N finds the item closer to the end of the file.

4. Open File with Pattern Search

Use the -p option to open a text file on the page containing the first item that matches the specified pattern. The search is case-sensitive.

Note: Another great tool that facilitates pattern searching is grep. Learn how to use grep to search for multiple patterns, words, or strings.

For example, running the following command finds all items containing the "ERROR" string in the mysql.conf file:

less -pERROR /etc/init/mysql.conf
Open a file in less and search for a pattern.

Note: When specifying the pattern, ensure there is no whitespace between the -p option and the pattern.

5. Remove Multiple Blank Lines

The -s option squeezes multiple blank lines from a text file into one blank line. Removing multiple blank lines allows less to show more content in each screenful of the file.

For example, the following file has multiple blank lines between lines of text:

less welcome.txt
Opening a file with multiple blank lines.

Specifying the -s option squeezes the blank lines into one:

less -s welcome.txt
Squeeze multiple blank lines into one using less.

6. Open Multiple Files

Open multiple files simultaneously using less without losing the current position in the files. To open multiple files, specify file names one after another. For example:

less welcome.txt aboutus.txt

The less command opens all the specified files and shows which file you are currently viewing at the bottom of the screen:

Opening multiple files using less.

Move to the next file by pressing the : key followed by n.

Moving between multiple files in less.

Return to the previous file by pressing : and p.

7. Mark Text

Use marks in less to mark an interesting section or passage in a file to return to it later quickly. Marks are a kind of flags.

Add a mark by selecting text and pressing the m key, followed by a letter of your choice. To add more marks, use different letters.

For example:

Mark a text selection in less.

Return to a mark by pressing ' followed by the letter used to mark the section.

8. Keep Content on Screen After Quitting

After quitting less, the terminal window clears, removing the file output. To leave the file contents in the terminal after quitting, specify the -X option.

For example:

less -X /etc/init/mysql.conf
Keep the less command output on the terminal after quitting.

As the example above shows, the file contents remain on the terminal after quitting less.

9. Real-Time Monitoring

The +F (forward) option is a real-time monitoring mode in less. Use the +F option to make less display the latest messages or lines being added to a file in real-time.

The + option flag instructs less to treat the option as if it were used inside less. If a file is already opened in less, engage forward mode by pressing the F key.

For example, the following command shows the latest messages in the system log file:

less +F /var/log/syslog
Invoking less with forward mode to monitor a file in real-time.

less displays a message that it is waiting for new data. The terminal automatically scrolls down for new messages.

Quit forward mode and return to the standard less interactive mode by pressing Ctrl+C.

Note: Scrolling and paging are disabled in the forward mode because less displays only the bottom of the output and waits for new messages.

10. View Piped Input

Use pipes to process output from other commands via less. Piping into less is especially useful when the output is long and clutters the terminal.

For example, the dmesg command displays kernel-related messages and its log file can be quite large and flood the terminal. For easier navigation in the file and better readability, pipe the dmesg output into less:

sudo dmesg | less

Refresh the output and see the latest messages by pressing the End key. Alternatively, specify the +F (forward) option or press F while in less to automate the process and have less always show new data when it arrives.

For example:

sudo dmesg | less +F
Pipe another command's output into less.

The output shows the last page of the file and waits for new data.

11. Edit Files

While less only allows you to view files, it compensates for the lack of file editing options with a shortcut. While viewing a file in less, press v to transfer the file to the system default text editor. Leaving the editor reopens the file in less.

For example, the default text editor in Ubuntu is nano.

12. Show Statistics

Press the = key while in less to see more information about the file and its location. Alternatively, specify the -M option to invoke less in verbose mode.

The option shows which lines are currently displayed, the progress into the file, and the file size.

For example:

Get file statistics in less.

Note: If the file is very long, less might take some time to gather the statistics. Stop the process by pressing Ctrl + C.

If you want to see more information from a pipe, = shows only what it knows, i.e., it will not show the number of lines and bytes until it reaches the end of the file.

Conclusion

This guide showed how to use the less command in Linux. Although there are other terminal pagers, such as most and more, less could be a better choice as it is a powerful tool present in almost every system.
Next, learn about the Linux head command or visit our overview of Linux commands where you can find the list of all important commands in one place.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
List Installed Packages on Ubuntu
March 9, 2022

In this tutorial, learn how to list all installed packages with apt and dpkg, save the list to a file or list specific packages.
Read more
How to Use the Linux history Command
March 3, 2022

Learn how to manage the Bash list of previously used commands and reuse them using the Linux history command.
Read more
Linux File Command: How to Determine File Type in Linux
March 3, 2022

The file command in Linux determines the type of a file. Learn how to use the file command in this tutorial.
Read more
How to Show or Hide Line Numbers in Vim
February 28, 2022

Learn to activate line numbering in Vim by showing absolute, relative or hybrid lines in a couple of simple steps.
Read more