less Command in Linux Explained [With Examples]

Last updated: July 9, 2023 | Linuxopsys

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.

OptionsDescription
-NPrint the line number at the beginning of the each line.
-IIgnore the case for all searches.
-MShow range of page numbers and percentage through the file at the bottom of the screen.
-mPrint the line number at the beginning of each line.
-EExit the file without the need to type Q in less.
-SDo not wrap long lines.
-XDefault less clear the terminal. This keeps the content in terminal after exiting less.
+FSimilar to tail -f. Track additional data at the end of the file in real-time.
-p patternTell less to go the start of first occurrence of the pattern.
-RPrint 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
viewing file content using less command

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 CommandsDescription
Up arrow, y or kMove up (backward) by one line.
Down arrow or jMove down (forward) by one line.
SpaceMove down by one page.
b or BMove up by one page.
dMove down by half a page.
uMove up by half a page.
Right  Arrow KeyScroll horizontally to the right
Left Arrow KeyScroll the page Horizontally to the left
G or >Go to the end of the file
g or <Go to the start of the file
ngMove 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+FNavigate forward one window
Ctrl+BNavigate backward one window
Ctrl+GDisplay filename, lines showing, bytes, and percentage statistics
Shift + FUsed inside less. This track additional data coming to the end of the file.
:nMove to the next file
:pMove to the previous file
h or HDisplay summary of less commands while na
/patternSearch forward in the file for pattern.
?patternSearch backward in the file for pattern.
nRepeat the last search (in the same direction).
NRepeat the last search (in the opposite direction).
=Detailed prompt including bytes. Similar to -M option
q or QMove 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
search a pattern within less

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
case insensitive search using capital i option

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 
show line numbers in each line of the file using less

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
go to 10th line of the file as well as showing line number in less

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
viewing multiple files in less

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
less -X option

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'.

set a mark

To go to the mark press 'f from the prompt:

go to the reference mark

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
less +F tracking additional data at end of file in real time.

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).

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment