The less command is used in Linux to view the contents of a file in a terminal. This content will be shown on one screen at a time. Unlike editors (such as vim, nano, etc.) which are used for editing, the less command is used for presenting the content, scrolling backward and forward, searching patterns, and viewing multiple files.
less is the preferred command for viewing large files when compared with more and cat. This is only because of its enhanced features of navigation tasks. When compared to more command, less has more options and navigation shortcuts.
In this guide, we learn about less command in Linux with examples.
Syntax and Options
The following is the basic syntax of less command.
less [Options] [filename ...]
- [Options] - Used to change the behavior of less.
- [Filename] - The name of the file you want to view the contents. You can also add multiple filenames.
Options
The following table lists some of the useful options with less.
Options | Description |
---|---|
-N | Print the line number at the beginning of the each line. |
-I | Ignore the case for all searches. |
-M | Show range of page numbers and percentage through the file at the bottom of the screen. |
-m | Print the line number at the beginning of each line. |
-E | Exit the file without the need to type Q in less. |
-S | Do not wrap long lines. |
-X | Default less clear the terminal. This keeps the content in terminal after exiting less. |
+F | Similar to tail -f. Track additional data at the end of the file in real-time. |
-p pattern | Tell less to go the start of first occurrence of the pattern. |
-R | Print the line number at the beginning of each line. |
Getting Started with less
To open and view a file with less:
less filename
You can replace filename with the name of the file you want to view.
Example:
less /etc/ssh/ssh_config
This command opens the file named /etc/ssh/ssh_config in less and allows you to navigate through it.
Navigation
The basic navigation controls you should know are:
- Space bar: Scroll down one page.
- b: Scroll up one page.
- Up arrow or k: Move up one line.
- Down arrow or j: Move down one line.
- g: Go to the beginning of the file.
- G: Go to the end of the file.
To exit less, you simply press q.
The following table shows an overview of navigation shortcuts of less:
Keyboard Commands | Description |
---|---|
Up arrow, y or k | Move up (backward) by one line. |
Down arrow or j | Move down (forward) by one line. |
Space | Move down by one page. |
b or B | Move up by one page. |
d | Move down by half a page. |
u | Move up by half a page. |
Right Arrow Key | Scroll horizontally to the right |
Left Arrow Key | Scroll the page Horizontally to the left |
G or > | Go to the end of the file |
g or < | Go to the start of the file |
ng | Move to the nth line (ie nth line will be at the top of the screen). Type this after opening the file. Don't press Enter. |
Ctrl+F | Navigate forward one window |
Ctrl+B | Navigate backward one window |
Ctrl+G | Display filename, lines showing, bytes, and percentage statistics |
Shift + F | Used inside less. This track additional data coming to the end of the file. |
:n | Move to the next file |
:p | Move to the previous file |
h or H | Display summary of less commands while na |
/pattern | Search forward in the file for pattern. |
?pattern | Search backward in the file for pattern. |
n | Repeat the last search (in the same direction). |
N | Repeat the last search (in the opposite direction). |
= | Detailed prompt including bytes. Similar to -M option |
q or Q | Move to the nth line of the output |
Searching Within Files
In less, you have the ability to search within files. Following are the search shortcuts.
/pattern
: Search forward in the file for pattern.?pattern
: Search backward in the file for pattern.n
: Repeat the last search (in the same direction).N
: Repeat the last search (in the opposite direction).
Example:
To search forward for the string named Port within the file /etc/ssh/ssh_config, open the file with less and type:
/ssh
Remember the search starts from the current location in the file. It will highlight all matches found on the page. Press n
to continue to the next match of the search term in the forward direction. If you want to search in the reverse direction (up the file), press N
.
If it doesn't find any more matches, it will display a message like Pattern not found (press RETURN)
at the bottom of the screen.
By default search function within less is case-sensitive. In our above example, less won't match Ssh or SSH. You can change the behavior using -i
or I
options. When the -i
option is used, case is ignored in searches unless the search pattern contains an uppercase letter (In our example /Ssh won't match). Instead, you can use -I
to search completely case insensitive.
If you want to search backward use ?
instead of /
.
?ssh
Just like when searching forward, you can n ( next match in reverse) and N ( for forward search).
Another method of pattern matching is using -p option. This shows the first occurrence of a specific pattern.
Example:
less -p Ssh -I /etc/ssh/ssh_config
Here we have combined multiple options - used -p to search pattern Ssh case-insensitively.
Line Numbers and less
In less, use the -N
option to display line numbers. The line number will be displayed at the beginning (left side) of each line.
Example:
less -N /etc/ssh/ssh_config
This command will open the SSH configuration file with less, and display line numbers next to each line.
Go to a specific line number
In less, you can use two methods to go to a specific line number.
To navigate from within the file using ng. This shows the nth line of the file to position at the top of the page.
Instead of navigating, you can directly specify the line number when opening the file.
Example
less +10 filename
You can add -N option to confirm this.
less +10 -N /etc/ssh/ssh_config
Working with Multiple Files
You can open multiple files in less by passing each filename as arguments.
Syntax:
less [Filename1] [Filename2] …..
Example:
less /etc/ssh/ssh_config /etc/rsyslog.conf
Now you may wonder how to navigate between these two files. Use :n
to move to the next file and :p
to move to the previous file. In case you forgot where you are, you can use :f
display the status of the current file ( especially showing the filename currently viewing).
Display content on the terminal after exited
By default after exiting from less the content of the file won't be visible on the terminal. You can change this behavior by using -X option - this enables less to keep the content on the terminal even after you have exited.
Example:
less -X /var/log/dmesg
Here you can see the content of file /var/log/dmesg till remain on the terminal. If it is a big file you can scroll up using the terminal bar on the right side to navigate the portion of the file you have viewed before.
Marking Positions
In less you to mark positions in a file and can go back to that position using the reference.
You may insert a mark by typing 'm' followed by a reference character. If you want to go back to that marked position, use a single apostrophe (') followed by the character typed previously.
For example, press m
when you need to set mark a place, type any letter as an identifier, here I pressed the letter 'f'.
To go to the mark press 'f
from the prompt:
Less +F
The +F option in less enables tracking the new lines being added to the end of the file. This is something similar to tail -f command.
When you run less +F filename, less command wait for additional data, and as soon as it comes shown in real-time. Generally, this comes useful for monitoring log files for errors. If already inside less you can use SHIFT + F to track the file. To stop tracking press Ctrl + C and you can press +F again to tell less to wait for data. When finished tracking press Q to exit from less.
Example:
less +F /var/log/dmesg
Once the tail -f is closed it cant scroll back to see data on the terminal. Whereas for less +F you can navigate (after pressing CTRL + C).
Comments