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.
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.
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:
Option | Description |
---|---|
-E | less automatically exits upon reaching the end of file. |
-f | Forces less to open non-regular files (a directory or a device-special file). |
-F | Exit less if the entire file can be displayed on the first screen. |
-g | Highlights the string last found using search. By default, less highlights all strings matching the last search command. |
-G | Removes all highlights from strings found using search. |
-i | Ignores case sensitivity during search. |
-J | 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. |
-m | Instructs less to prompt verbosely (similar to more ), showing the percentage into the file. By default, less prompts with a colon. |
-M | Instructs less to prompt even more verbosely than more . |
-n | Removes line numbers from the screen. |
-N | Displays 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. |
-Q | Enforces quiet operation that silences the terminal bell. |
-s | Merges consecutive blank lines into a single blank line. |
-X | Disable 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
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:
Shortcuts | Action |
---|---|
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. |
Right Arrow | Scroll right. |
Left Arrow | Scroll left. |
Home, g | Jump to the beginning of the file. |
End, G | Jump to the end of the file. |
/[string] | Search forward for the specified string. |
?[string] | Search backward for the specified string. |
n | Next match during a search. |
N | Previous match during a search. |
q | Quit less. |
For a detailed list of navigation options, run:
less --help
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.
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
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 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.
Note: Learn to search for a string using Vi or Vim.
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
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
Specifying the -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
The 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 n
.
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:
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
As the example above shows, the file contents remain on the terminal after quitting less
.
Note: Learn how to use the less
command to open a file in Bash.
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
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
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:
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.