In this tutorial, I will go through how you can use the less command on a Linux operating system.
The less command allows you to display the content of a file or output in smaller parts. This command is particularly useful when viewing large files with hundreds of lines of text. The command is similar to the more command but offers a few more features.
One of the main benefits of using less is that it does not read the entire file before outputting data. This behavior will present much faster start-up times on large files when compared to using text editors such as nano or vi.
You can use other commands, such as the cat command to review files. However, you will find that cat is better suited for smaller files as it will print the entire file to the terminal.
less Command Syntax
The less command syntax is somewhat simple unless you use many additional options. Below is a basic example of how to enter the command into the terminal.
less [OPTIONS] [FILE]
- [OPTIONS] is where you can specify the many options you can use. We touch on some options you can use further down this page.
- [FILE] is the file you wish to read using the less command.
less Command Options
There are a lot of different options that you can use with the less command. For this tutorial, we will touch on a few of them, but you may need to refer to the manual for information on all the options available.
--QUIT-AT-EOFwill have the command quit once you reach the end of the file. The same command in lowercase will automatically exit when you reach the end of the file for the second time.
--forcecan be used to force non-regular files to be opened, such as a directory or a device special file. It will also suppress the warning message when a binary file is opened.
--quit-if-one-screenwill have the less command exit automatically if less can display the entire file on one screen. This option will keep the file contents displayed on the screen upon exit.
--ignore-casewill have the search ignore case.
--LINE-NUMBERSwill force a number to be displayed at the start of each line. Using the same option but in lowercase will suppress line numbers.
--pattern=[pattern]is the same as specifying
/pattern. It tells the command to start at the pattern’s first occurrence.
--squeeze-blank-lineswill compress consecutive blank lines to a single blank line. Useful when you have files with a large number of blank lines.
--no-initwill prevent the clearing of the screen by disabling sending termcap initialization and deinitialization.
--versionwill display the version information for the less command on your Linux system.
+Fwill allow you to monitor files in real-time. It makes the less command similar to the tail command.
I recommend you refer to the manual for this command, as there are many more options that you can use. However, the options I mentioned above will likely cover most typical use cases.
Navigating within Less
There are numerous hotkeys and commands that you can use to navigate a file while using the less command. We will touch on some of the basics below.
Many of the hotkeys and commands you can use are similar to the more command or the vi editor.
- e, j, ENTER, DOWN ARROW will move forward one line.
- y, k, UP ARROW will move backward one line.
- LEFT ARROW and RIGHT ARROW will move one-half screen width left or right.
- f or Spacebar will move one window forward.
- b or ESC + v will move one window backward.
- F will move forward forever. Similar to using
- R will reload the screen.
/patternsearches forward for the line that matches the pattern.
?patternsearches backward for the line that matches the pattern.
&patternonly displays lines that match the pattern.
- n repeat the previous search. Using a capital N will search in the reverse direction.
- ESC + u will undo the search highlighting.
- g or < will go to the first line in the file. Entering a number before pressing g will take you to that specific line. For example,
100 gwill take you to line 100.
- G or > will go to the last line in the file. Entering a number before pressing G will take you to that specific line. For example,
25 Gwill take you to line 25.
- p will take you to the start of the file. If you enter a number before pressing p, it will take you to that percentage in the file. For example,
50 pwill take you 50 percent into the file.
[LETTER]will mark the current top line with the specified letter. Using a capital M will mark the bottom line.
[LETTER]will jump you to the previously marked position.
- ' ' will go to the previous position.
- = or CTRL + G will print the file name and statistics about the file at the bottom of the terminal.
- : + e
[file]allows you to change to a new file.
- h displays helpful information on the different hotkeys and commands you can use.
- q quits out of the less command.
Again, these are only some of the hotkeys you can use within less. If you want to know the complete list, consult the help or manual pages on your Linux operating system.
Using the less Command
There are many different ways that you can use the less command, so we will only cover a few examples that you will likely use.
Viewing a File
The easiest way to use the command is to load a file. The example below will load and display part of the dmesg log file on our operating system.
Depending on the file size, you should be able to navigate it by using the arrow keys on your keyboard.
Display Line Numbers
Line numbers can make navigating, reading, and bookmarking a large file much easier. Luckily it is super easy to activate line numbers while reading a file using the less command.
--LINE-NUMBERS options will output line numbers for every line within the file. Below is how you can structure the command correctly.
less -N /var/log/dmesg
Search for Text
You can search for text within a file using the less command in a few different ways. To include the search within the command, you can use the
Below is an example of performing a search “
Listening on” on the dmesg log file.
less -p 'Listening on' /var/log/dmesg
You can also perform a search while you are within the file.
If you enter /, you can enter a phrase you wish to search after the slash. This search will look forward for the matching line. Below is an example of how the input should look.
Entering ? will allow you to enter a phrase to search. This search will look backward for the matching line. The example below demonstrates how you should enter it into the less program
Using & also allows you to enter a phrase to search. This search will look over the entire file but only display lines that match the search phrase. Below is an example of how it would look if you were to enter it into your computer.
Marking Locations in the Text File
You can mark different locations in a file so that you can quickly navigate it. However, as soon as you close out of the file, all the marked locations will be lost, so keep this in mind when using this feature.
To mark a location, enter m followed by a letter. For example, after pressing m, your terminal should say the following.
Pressing a letter will save the current position to the specified letter. For example, pressing p will save the location to the p letter.
Using lowercase m will save the top-line position while capital M will save the bottom-line position.
After setting a mark, you can return to it by pressing ' followed by the letter you used. For example, if I used the letter a, I would enter the following to jump to my saved mark.
You can use the
+F option with the less command to continually monitor a file. This method is the same as using the tail command.
The example below will load and monitor the dmesg log for new data.
less +F /var/log/dmesg
At the bottom of the screen, you should see some text that says something like “Waiting for data… (interrupt to abort)”.
Exit the Less Command
If you find yourself stuck within a text file, you will be happy to know that it is super easy to exit.
To exit the current text file, you can use the q key. If that doesn’t work, capital Z twice will also exit out of the software.
This command is easy to use, but if you require to use some of the more advanced features, you may need further help. Below are two of the easiest ways to get a better explanation of what some of the hotkeys and options can perform.
The more in-depth option is to use the manual command available on almost every Linux distribution. Simply enter man followed by the command, as shown in the example below.
The other method for getting more help is to use the
--help option with the command. Below is an example of getting some help displayed to the terminal.
I hope you now have a good understanding of the less command and how you can utilize it effectively. You will likely need to use this command if you constantly review large files on a Linux system.
If you are new to Linux, I recommend you check out some of our other Linux command tutorials. For example, the ls command and cd command are useful for navigating directories on a Linux-based operating system.
Please let us know if you notice a mistake or if an important topic is missing from this guide.