In Linux, the “less” command is a powerful pager utility that allows users to view and scroll through the contents of a file or the output of a command. It is a more advanced version of the “more” command, with additional features. These features include searching text, navigating backward and forward through the file, and displaying the file contents in different ways.
This article will offer the “less” command with different implementations to search in an instant manner.
- How to Instant Search in less Command?
- Search for a Specific Word
- Case-insensitive Search
- Search for a Regular Expression
- Search Multiple Files
How to Instant Search in less Command?
One of the key features of the “less” command is its ability to search for text within a file. It provides a “backward search” function, which allows users to search for text in the backward direction (from the current position of the cursor to the beginning of the file).
To instant search for a text using the “less” command, follow different examples:
Example 1: Search for a Specific Word
To search for a specific word within a file, first use the “less” command by specifying the filename. In our case, specify the existing file named “file.txt” that is present in the home directory:
$ less file.txt
Once the file is open, start searching for text by typing a forward slash (/) followed by the text to search. For instance, search for the word “World” in the “file.txt”:
Press the “Enter” button to start the search. To search for the next occurrence of the term, press the lowercase letter “n”. To search for the previous occurrence of the term, press the uppercase letter “N”:
The output highlights the occurrences of the search term “World” in the file. To exit the search mode, press the lowercase letter “q”.
Example 2: Case-insensitive Search
By default, the “less” command searches for text in a case-sensitive manner. However, users can perform a case-insensitive search by using the “-i” option:
$ less -i file.txt
For instance, search for the word “world” in a case-insensitive manner in the below script:
The output shows all the occurrences of “world” in a case-insensitive manner.
Example 3: Search for a Regular Expression
The “less” command allows users to search for text using regular expressions. For instance, search for all lines that contain the word “linux” followed by 1 to 5 number of digits:
The output shows all the “linux” occurrences, including digits 1 to 5.
Example 4: Search Multiple Files
To search a text in multiple files at once, specify the file names after the “less” command and separate them by spaces. For instance, specify the filenames “file.txt” and “file2.txt” in the below script:
$ less file.txt file2.txt
It opens the specified files in the terminal. After that, search the word “linux” with the forward slash as below:
It searches out the “linux” word in “file.txt” and “file2.txt” files and highlights them as seen in the above terminal.
To explore more options for the “less” command, follow our article “less Command in Linux”.
To instant search with the “less” command, use the “less <filename>” script by specifying the </searched_text> in the Linux terminal. It allows users to quickly locate the specific text within a file and navigate through the file with ease. The “less” command also provides search assistance based on case-insensitive, regular expressions or in multiple files.
This article has demonstrated various examples of using the instant search in the less command.