Sep 18, 2023 4 min read

Less Command in Linux

In this tutorial, we will discuss the less command that displays the contents of a file or command output, one page at a time

Less Command in Linux
Table of Contents

Introduction

Less is a command-line utility that displays the contents of a file or command output, one page at a time. It's comparable to more, but it offers more advanced capabilities, like the ability to traverse through the file both forward and backward.

When compared to text editors like vim or nano, less does not read the complete file when starting up, resulting in significantly faster load times. The less command is typically used to open large files.

In this tutorial, you will understand less command in Linux. We will also address a few FAQs on less command in Linux.

How to Use Less

The less program's general syntax is as follows:

less [OPTIONS] filename

For example, to see the contents of the GPL-3 file in /usr/share/common-licenses, type:

less /usr/share/common-licenses/GPL-3

A pipe can also be used to route the output of a command to less. To see the output of the ps command page by page, for example, type:

ps aux | less

When you open a file with material that is too long to fit on a single page, you'll see a single colon (:).

To advance to the next page, use the f key or the Space bar. Type the number followed by the space or f key to move down for a certain number of lines.

You can scroll ahead by one line by pressing the Down arrow or Enter, and backward by one line by pressing the Up arrow.

Press the b key to return to the previous page. By typing the number followed by the b key, you can move ahead for a certain number of lines.

If you wish to look for a pattern, start typing forward-slash (/).and then the pattern you want to look for. When you press Enter, less will look for matches in the future. Use (?) followed by the search pattern to search backward.

The string (END) appears at the bottom of the screen when the file has reached its end.

Press q to return to the command line and quit less.

less Options

Start the program using the -N option if you wish to see fewer line numbers:

less -N filename

When less exits, the contents of the file are removed from the screen by default. Use the -X option to keep the contents of the file on the screen:

less -X filename

The +F option instructs less to keep an eye on the contents of the file for changes. When opening log files, this comes in very handy.

less +F /var/log/messages

Less behaves almost identical to tail -f when started with +F.

less Commands

The less application includes a variety of commands that allow you to search for strings and traverse through the file content. Type h to see a complete list of all commands.

The majority of the commands you can type from the keyboard are inspired by those used by both more and vi. Different keys can be used to perform the same action.

When viewing a file with less, the following are some of the most commonly used commands to traverse the content:

Command Action
Down arrow, Enter, e or j Move forward one line
Up arrow, y or k Move backward one line
Space bar or f Move forward one page
b Move backward one page
/pattern Search forward for matching patterns
?pattern Search backward for matching patterns
n Repeat previous search
N Repeat previous search in reverse direction
g Go to the first line in the file
Ng Go to the N-th line in the file
G Go to the last line in the file
P Go to the beginning of the file
Np Go to N percent into file
h Display help
q Exit less

FAQs on less command in Linux

How do I use the less command to view a file? 

Simply type less filename in the terminal to open a file in less mode. For example, less myfile.txt will open the file myfile.txt for viewing.

How can I navigate through a file using less? 

To navigate through a file in less, you can use arrow keys or the Page Up and Page Down keys to scroll up and down. Additionally, you can use the Spacebar to move forward and the letter "b" to move backward.

Can I search for specific text within a file using less? 

Yes, you can search for text by pressing the "/" key followed by your search term and then pressing Enter. Use "n" to go to the next occurrence or "N" to go to the previous occurrence.

Are there any other helpful text manipulation options in less? 

less provides various useful options. For example, pressing "G" takes you to the end of the file, and typing a line number followed by "G" takes you to a specific line.

How can I exit the less command once viewing a file? 

To exit less, simply press the "q" key. This will return you to the terminal prompt.

Can I view multiple files using less? 

Yes, you can view multiple files by listing them after the less command: less file1.txt file2.txt. You can navigate through the files using the :n and :p commands.

Can I view the contents of compressed files with less? 

Yes, less can display the contents of compressed files like .gz or .bz2. You can use less filename.gz or less filename.bz2 to view the compressed file.

Conclusion

You should have a firm grasp on how to utilize the less command by now.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.