There are ways to read text files in Linux command line. Cat is perhaps the most elementary command that almost every Linux user knows in this regard. But cat is not always the best option for displaying the content of a file.
Imagine that you have a text file with over a thousand line. Using the cat command will simply fill the entire terminal screen with those lines. You cannot perform any more operations like searching for a particular text after you have used the cat command here.
This is where less command helps you big time.
What is the less command in Linux?
With less, you can read large text files without cluttering your terminal screen. You can also search for text and monitor files in real time with it.
Some people prefer using Vim for reading large text files. But less is faster than Vim or other such text editors because it doesn’t read the entire file before starting. Since less is ‘read only‘, you don’t have the risk of accidentally editing the files you are viewing.
The syntax for the less command is extremely simple:
There are numerous options with less command but it’s better to focus on the practical usage that will be more useful for you.
Let’s see some of the most useful examples of less command in Linux.
Practical examples of less command in Linux
Let’s see how to use the less command in Linux with some practical examples.
It is better to work with a big file to understand the usage of the less command. Instead of creating a huge text file by hand, I would advise copying the existing file from /etc/services to your home directory or wherever you want to practice these commands.
/etc/services is a big file with hundreds of line and once copied, you can use it for your practice.
1. View a text file with less
As showed in the syntax, you can use the less command to view a file in the following fashion:
The output will be something like this:
Note: If you view a short text file with less, you would see empty blank lines at the top. Don’t panic. There are no extra lines in your file. It’s just the way less displays them.
2. Exit from less
If you are not used to of less command, you might struggle to find how to exit less. Trust me it’s not at all complicated. Simply press ‘q’ at any given point to exit from less.
I added the exiting before so that you may follow the rest of the less command examples easily as you would need to exit the files between different examples (if you are practicing the commands while reading this article).
3. Moving around in less
The output of less is divided into sort of pages. You’ll see only the text that fills up to your terminal screen.
You can use the up and down arrow keys to move line by line. If you want to move page by page, use space key to move to next page and ‘b’ key to go back to the previous page.
If you want to move to the beginning of the file, use ‘g’ key. If you want to go to the end of the file, press ‘G’ key.
- Up arrow – Move one line up
- Down arrow – Move one line down
- Space or PgDn – Move one page down
- b or PgUp – Move one page up
- g – Move to the beginning of the file
- G – Move to the end of the file
- ng – Move to the nth line
4. Display line numbers with less
If you want to see the line numbers in the less command output, you can use the option N in the following manner:
less -N <filename>
You’ll see an output like this:
5. Finding text in less
If you have a large text file, it’s better to search for a specific piece of text rather than reading it line by line in order to find it manually.
To find a word or phrase or even a regex pattern, press / and type whatever you want to find.
As you can see, the matched text is highlighted.
If there is more than one match, you can move to the next matched text by pressing ‘n’ key. You can move back to the previous match with ‘N’ key.
The ‘/pattern’ performs a forward search. You can use ‘?pattern’ to perform a backward search. Personally, I prefer doing a forward search and then pressing n or N to cycle through all the matches. No need to worry about a forward or backward search.
By default, search in less is case sensitive. To ignore case, you can use less with -I option
less -I <filename>
If you forgot to use this option, don’t worry. You can also press ‘-I’ key combination before performing a search inside less.
&patternit will display only the lines that match the pattern.
6. Marking interesting points
While you are scrolling through a big text file and you find something interesting, but you also need to continue checking the file, how would you remember that interesting point? The answer is marking.
Less allows you to add marks, sort of flags, to any line. You can add a mark by pressing the key ‘m’ followed by a letter.
When you want to go back to this mark, simply press ' followed by that letter:
You are not limited to a single mark point. You can add multiple marks in less. Simply use a different letter for each marked position like mb, mc etc.
Do note that this bookmarking is not permanent. You'll lose it as soon as you exit the less command.
7. Monitor files in real-time with less command
You can also do the same with less command. You can see the new lines being added to a file in real-time by using the +F option.
less +F <filename>
It will display the last page of the file and then wait for the new data to be added. Note that you cannot perform the regular moving up and down, back and forth in this mode.
To exit the real time monitoring of log files, press Ctrl+C. With this, you will be back to the normal view of the less command and can exit the file normally by pressing the Q key.
8. View multiple files with less command
I’ll be honest with you. This is not my favorite less command example but you can totally do this.
To open multiple files with less, simply input the file names one by one:
less <filename1> <filename2> <filename3>
You’ll see that it lists the file name along with its position in the list of the files.
You can view other files in the list using these keys:
- :n – view the next file in the list
- :p – view the previous file in the list
9. Using less command with pipes
The less command can be used in conjugation with other commands using pipes. It is particularly useful when you know that the output of a certain command is going to be huge.
For example, the output of dmesg command is usually in thousands of lines. You don’t want it to flood your screen and you won’t be able to analyze the output as well. Pipe it with less and you’ll have a more friendly way of reading this output.
dmesg | less
Bonus Tip: Edit a file with less command in Linux
No, you cannot do that. You cannot edit a file in less.
One of the biggest advantages of less command is that it provides a ‘read-only’ view.
If you cannot edit text with less then why did I add this as a less command example? Because when you feel like you need to edit the file you are viewing, simply press the ‘v’ key.
It will open the file in the default command line text editor of your Linux system. For Ubuntu-based system, it should be opened in Nano editor.
There is more with less
I hope you find these less command examples useful while using Linux. Of course, there could be many more usage of less commands. You can explore them by going through all the options of less command.
There is a similar ‘more command‘ that is also popular. I prefer using less because it is more user friendly.
If you have any questions or suggestions, please share them in the comments section below.