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
In this tutorial, you will learn to use the
less command in Linux.
- 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
[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.
Note: Learn to use the Bash shell to read a file line by line.
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:
less automatically exits upon reaching the end of file.
less to open non-regular files (a directory or a device-special file).
less if the entire file can be displayed on the first screen.
|Highlights the string last found using search. By default,
less highlights all strings matching the last search command.
|Removes all highlights from strings found using search.
|Ignores case sensitivity during search.
|Displays 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.
less to prompt verbosely (similar to
more), showing the percentage into the file. By default,
less prompts with a colon.
less to prompt even more verbosely than
|Removes line numbers from the screen.
|Displays line numbers at the beginning of each line.
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.
less to start at the first occurrence of the specified pattern in the input file.
|Enforces quiet operation that silences the terminal bell.
|Merges consecutive blank lines into a single blank line.
|Disable clearing the screen after quitting
|Changes the default scrolling window size to the specified
For a complete list of options, refer to the
less help file by running:
Navigating in the Text File
less command accepts keyboard shortcuts that facilitate text navigation, especially when reading large files. The following table comprises the most commonly used shortcuts:
|Down Arrow, Enter, e, j
|One line forward.
|Up Arrow, y, k
|One line backward.
|Space bar, Page Down
|One page forward.
|Page Up, b
|One page backward.
|Jump to the beginning of the file.
|Jump to the end of the file.
|Search forward for the specified string.
|Search backward for the specified string.
|Next match during a search.
|Previous match during a search.
For a detailed list of navigation options, run:
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
1. Open a Text File
Load a text file into
less by specifying the file path.
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.
Use the navigation shortcuts to move forward, backward, or search for specific strings in the file.
2. Show Line Numbers
-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.
less -N /etc/init/mysql.conf
The file opens, and each line in the file is numbered.
3. Search for a String
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
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
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 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.
Note: Learn to search for a string using Vi or Vim.
4. Open File with Pattern Search
-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
Note: When specifying the pattern, ensure there is no whitespace between the
-p option and the pattern.
5. Remove Multiple Blank Lines
-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:
-s option squeezes the blank lines into one:
less -s welcome.txt
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
less command opens all the specified files and shows which file you are currently viewing at the bottom of the screen:
Move to the next file by pressing the
: key followed by
Return to the previous file by pressing
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.
Return to a mark by pressing
' followed by the letter used to mark the section.
8. Keep Content on Screen After Quitting
less, the terminal window clears, removing the file output. To leave the file contents in the terminal after quitting, specify the
less -X /etc/init/mysql.conf
As the example above shows, the file contents remain on the terminal after quitting
Note: Learn how to use the
less command to open a file in Bash.
9. Real-Time Monitoring
+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.
+ 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
For example, the following command shows the latest messages in the system log file:
less +F /var/log/syslog
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
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
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.
sudo dmesg | less +F
The output shows the last page of the file and waits for new data.
11. Edit Files
less only allows you to view files, it compensates for the lack of file editing options with a shortcut. While viewing a file in
v to transfer the file to the system default text editor. Leaving the editor reopens the file in
For example, the default text editor in Ubuntu is nano.
12. Show Statistics
= 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.
Note: If the file is very long,
less might take some time to gather the statistics. Stop the process by pressing
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.
This guide showed how to use the
less command in Linux. Although there are other terminal pagers, such as
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.