Command Linux

How to use grep to search recursively

The “grep” instruction has been very well known among Linux users for its searching capabilities. The “grep” query works differently while used with different flags for recursive search. The most used flag of grep command is “-r” which implies to search in the current directory. Let’s have some examples of doing a recursive search in Ubuntu 20.04 using the Grep command. Start from launching the Terminal application via the shortcut key “Ctrl+Alt+T”.

Search Without Directory Path

The most simple and easiest way for recursive search is to use a simple “-r” flag within the grep command and a matching pattern without any path mentioned. We have been using the grep command to search for a pattern “John” within the files. The “–r” flag will search all the files located within the home or current directory of Ubuntu 20.04 and in the subfolders of the home directory as well. You can see it shows the file paths that have the particular match and displays the matched text within that file as well. The first two files are located within the Documents subdirectory of the home folder, while the other two are located in the home directory. This means it has searched from every subfolder of the current directory as well.

$ grep –r “John”

Let’s recursively search for another word, “heart” by utilizing the grep instruction with the “-r” flag. The command output shows that it has been found in the two files. One file resides within the home directory while the other is in the subdirectory of home, i.e., Documents. Both files contain the same name.

$ grep –r “heart”

Let’s take a look at another same instance with a different matching pattern within the grep command. So we have been using the pattern “CSS” to be searched from the system recursively.

$ grep –r “CSS”

Another way is to use the “*” sign to search all over the folders and subfolders. So, we have searched the simple character “a” and found it in every text file of the home directory and its subfolders.

$ grep –r “a” *

Let’s have a recursive search using the grep command but in a different way. We will be searching a pattern while the text in the files will not be displayed in the terminal using the “-l” flag in the instruction. Only the file name or path will be given in the output that shows that the given pattern has been found. So, we have tried the same query with the addition of the “-l” flag to search for the pattern “CSS”. It displays the related files and their paths in the output.

$ grep –r -l “CSS”

Search With Directory Path

Now, we will see the working of the grep recursive command on the directory path given. So, we will have searched for three different patterns within the subdirectory “Downloads” of the home folder. The path to the Downloads folders has been stated in the image within the grep command. We have got nothing in return as the pattern has not matched or been found.

$ grep –r CDD /home/linux/Downloads
$ grep –r CSS /home/linux/Downloads
$ grep –r poet /home/linux/Downloads

Let’s search for another pattern “is” within the Downloads folder by utilizing the same instruction. We have found two outputs for this pattern. The output displays the file paths along with the text. It also highlighted the pattern matched, i.e., “is”.

$ grep –r is /home/linux/Downloads

We have searched another pattern, “OK” within the Downloads folder and got only 1 output in return as it is only matched with a single file.

$ grep –r OK /home/linux/Downloads

The very same pattern “OK” has been searched from the different subdirectory this time, i.e., Documents. The grep command returned nothing as the folder doesn’t contain any file with a particular pattern “OK”  within it.

$ grep –r is /home/linux/Documents

You can also search recursively for the string value. So, we have provided the IP address as a string and mentioned the path. This command will search all the files and folders of the “etc” directory to search the string pattern. The output shows a single result.

$ grep –r “192.168.10.1” /etc/

Search Multiple Patterns

We had looked at the examples to search for the single pattern using the grep recursive instruction. Now, we will see the way to search multiple patterns as well recursively. So we have been utilizing the grep command along with the “-rw” flag. It indicates the multiple searches to have happened. After this flag, you can mention the path as well. We have been using it without the path to search within the home directory and its subfolders. The “-e” flag has been used here to indicate the single pattern. You can use many “-e” flags according to the number of patterns you want to search. We have searched only two patterns here using “-e”. We have found the 4 files in the output matched. Three files contain the pattern “CSS”, and 1 contains the pattern “OK” within.

$ grep –rw -e OK –e CSS

You can also utilize the grep command to search multiple patterns within the specific file. For this, you have to remove the “-rw” flag from the query and add “-e” flags as much as you want. But you have to mention the file name at the end, as demonstrated below. It displays all the text from the file and highlights the matching pattern.

$ grep –e “great” –e “CSS” –e “is” –e “heart” new.txt

Search with “Include” Flag

The include flag keyword has been used to specify the file types that are being searched only. No other files will be searched. So, in the first case, we couldn’t get anything.

$ grep –r –include=”*txt” “ok” /home/linux/
$ grep –r –include=”*txt” “NEW” /home/linux/

Within our second case, we have found a single output.

Another pattern, “CSS” has been used for recursive search and got three results, as demonstrated in the picture.

$ grep –r –include=”*txt” “CSS” /home/linux/

Conclusion

The recursive search has been demonstrated within this guide using the Grep command. The “grep” command has used many flags to work differently while doing a recursive search, i.e., “-r” and “include”. The tutorial also explains the recursive search with and without the directory paths given in the grep command. Thus, this article contains a bundle of easy-to-do examples for its users to learn recursive search most easily.

Related Articles

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Back to top button