- 9 Simple Ways to Effectively use Less Command in Linux
- What is the less command in Linux?
- Practical examples of less command in Linux
- 1. View a text file with less
- 2. Exit from less
- 3. Moving around in less
- 4. Display line numbers with less
- 5. Finding text in less
- 6. Marking interesting points
- 7. Monitor files in real-time with less command
- 8. View multiple files with less command
- 9. Using less command with pipes
- Bonus Tip: Edit a file with less command in Linux
- There is more with less
- Instant search in less command? (Similar to incsearch in VIM)
- 4 Answers 4
9 Simple Ways to Effectively use Less Command in Linux
Less is an awesome Linux command utility for viewing text files. Here are some essential less command examples to use it effectively.
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:
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
If you forgot to use this option, don’t worry. You can also press ‘-I’ key combination before performing a search inside less.
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 probably already know that you can use tail command to monitor log files in real-time.
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.
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:
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:
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.
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.
Instant search in less command? (Similar to incsearch in VIM)
Is there a way to search, while I’m typing in less ? Just like the vim option, set incsearch . As I didn’t find a proper way to do it, is there any similar tool that can do it?
@user1146332 hmm, I don’t know if VIM could handle a stream directly, but seems ire_and_curses has given the right way to use vim
4 Answers 4
You can do a search from the command line:
Or, once inside less , use / followed by your pattern to do interactive searching (forwards). n and N repeat the search in the forward and reverse direction, respectively. That’s the bare minimum you need to know; there are many more commands for more complex or specific searches.
Edit: To respond to your updated question, there’s currently no way to do immediate incremental searching with less . Have you considered using view instead (opens Vim in read-only mode, so will use your incsearch setting)? Vim can be made even more pager-like with the vimpager script.
Some additional information: There is an open bug on the Ubuntu bug-tracker for incremental search support, but it doesn’t look like it’s going anywhere soon. Somebody has implemented incremental support on a github fork, but obviously you’re going to have to compile a custom less to use that.
(And apart from the Ubuntu enhancement request there is currently (as of 2016-05-17) no such enhancement request on the official less bugtracker.)
+1 for recommending view (read-only vi) — i hate it when people use vi instead of less or view , and lock the file unnecessarily.
When I hit n , it adds n to the search pattern. Hitting enter terminates the search, after which hitting n has no effect. What to do?
It was implemented in https://github.com/gwsw/less/commit/b09a0c452d08afe2da8c9632d4fa768d6474b371 2 months ago, and hopefully will be coming to a distro near us!
Added in this commit on January 13 2021.
Version 598 was released for beta testing on 7 December 2021.
I add —incsearch to my LESS environment variable:
export LESS='--incsearch --ignore-case --status-column --LONG-PROMPT --RAW-CONTROL-CHARS --HILITE-UNREAD --tabs=4 --no-init --window=2 ' # Man-db passes extra options to the pager via the `LESS` environment variable, # which Less interprets in the same way as command line options. # The setting is hard-coded at compile time and starts with -i. # (The value is "-ix8RmPm%s$PM%s$" as of Man-db 2.6.2; the P…$ part is the prompt string.) # "--RAW-CONTROL-CHARS": Get color support for 'less' # --no-init: This is sometimes desirable if the deinitialization string does something unnecessary, like clearing the screen # --HILITE-UNREAD: highlight first unread line after forward movement # https://www.topbug.net/blog/2016/09/27/make-gnu-less-more-powerful/ source $HOME/dot_file/color_less_wf.zsh
These are the differences between version 590 and version 598:
Add the --header option. Add the --no-number-headers option. Add the --status-line option. Add the --redraw-on-quit option. Add the --search-options option. Add 'H' color type to set color of header lines. Add #version conditional to lesskey. Add += syntax to variable section in lesskey files. Allow option name in -- command to end with '=' in addition to '\n'. Add $HOME/.config to possible locations of lesskey file. Add $XDG_STATE_HOME and $HOME/.local/state to possible locations of history file. Don't read or write history file in secure mode. Fix display of multibyte and double-width chars in prompt. Fix ESC-BACKSPACE command when BACKSPACE key does not send 0x08. Add more \k codes to lesskey format. Fix bug when empty file is modified while viewing it. Fix bug when parsing a malformed lesskey file. Fix bug scrolling history when --incsearch is set. Fix buffer overflow when invoking lessecho with more than 63 -m/-n options. Fix display bugs with --header. Fix bug restoring color at end of highlighted text. Fix bug in parsing lesskey file.