How to make use of grep to go looking recursively


The “grep” instruction has been very widely known amongst Linux customers for its looking out functions. The “grep” question works otherwise whilst used with other flags for recursive seek. The maximum used flag of grep command is “-r” which suggests to go looking within the present listing. Let’s have some examples of doing a recursive seek in Ubuntu 20.04 the use of the Grep command. Start from launching the Terminal utility by means of the shortcut key “Ctrl+Alt+T”.

Search Without Directory Path

The most straightforward and best approach for recursive seek is to make use of a easy “-r” flag throughout the grep command and an identical trend with none trail discussed. We had been the use of the grep command to seek for a trend “John” throughout the recordsdata. The “–r” flag will seek the entire recordsdata positioned inside the house or present listing of Ubuntu 20.04 and within the subfolders of the house listing as smartly. You can see it displays the report paths that experience the precise fit and shows the matched textual content inside that report as smartly. The first two recordsdata are positioned throughout the Documents subdirectory of the house folder, whilst the opposite two are positioned in the house listing. This method it has searched from each and every subfolder of the present listing as smartly.

Let’s recursively seek for every other phrase, “heart” through the use of the grep instruction with the “-r” flag. The command output displays that it’s been discovered within the two recordsdata. One report is living inside the house listing whilst the opposite is within the subdirectory of house, i.e., Documents. Both recordsdata include the similar title.

Let’s check out every other similar example with a special matching trend throughout the grep command. So now we have been the use of the trend “CSS” to be searched from the gadget recursively.

Another approach is to make use of the “*” signal to go looking all over the place the folders and subfolders. So, now we have searched the easy persona “a” and located it in each and every textual content report of the house listing and its subfolders.

Let’s have a recursive seek the use of the grep command however otherwise. We will likely be looking out a trend whilst the textual content within the recordsdata may not be displayed within the terminal the use of the “-l” flag within the instruction. Only the report title or trail will likely be given within the output that displays that the given trend has been discovered. So, now we have attempted the similar question with the addition of the “-l” flag to seek for the trend “CSS”. It shows the comparable recordsdata and their paths within the output.

Search With Directory Path

Now, we can see the operating of the grep recursive command at the listing trail given. So, we can have searched for 3 other patterns throughout the subdirectory “Downloads” of the house folder. The trail to the Downloads folders has been mentioned within the symbol throughout the grep command. We have were given not anything in go back because the trend has now not matched or been discovered.

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

Let’s seek for every other trend “is” throughout the Downloads folder through the use of the similar instruction. We have discovered two outputs for this trend. The output shows the report paths together with the textual content. It additionally highlighted the trend matched, i.e., “is”.

$ grep –r is /house/linux/Downloads

We have searched every other trend, “OK” throughout the Downloads folder and were given only one output in go back as it is just matched with a unmarried report.

$ grep –r OK /house/linux/Downloads

The exact same trend “OK” has been searched from the other subdirectory this time, i.e., Documents. The grep command returned not anything because the folder doesn’t include any report with a selected trend “OK”  inside it.

$ grep –r is /house/linux/Documents

You too can seek recursively for the string worth. So, now we have equipped the IP deal with as a string and discussed the trail. This command will seek the entire recordsdata and folders of the “etc” listing to go looking the string trend. The output displays a unmarried outcome.

$ grep –r “” /and so on/

Search Multiple Patterns

We had regarded on the examples to seek for the only trend the use of the grep recursive instruction. Now, we can see the best way to seek a couple of patterns as smartly recursively. So now we have been using the grep command together with the “-rw” flag. It signifies the a couple of searches to have came about. After this flag, you’ll be able to point out the trail as smartly. We had been the use of it with out the trail to go looking inside the house listing and its subfolders. The “-e” flag has been used right here to suggest the only trend. You can use many “-e” flags in step with the selection of patterns you wish to have to go looking. We have searched most effective two patterns right here the use of “-e”. We have discovered the 4 recordsdata within the output matched. Three recordsdata include the trend “CSS”, and 1 accommodates the trend “OK” inside.

$ grep –rw -e OK –e CSS

You too can make the most of the grep command to go looking a couple of patterns throughout the explicit report. For this, you must take away the “-rw” flag from the question and upload “-e” flags up to you wish to have. But you must point out the report title on the finish, as demonstrated underneath. It shows the entire textual content from the report and highlights the matching trend.

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

Search with “Include” Flag

The come with flag key phrase has been used to specify the report sorts which are being searched most effective. No different recordsdata will likely be searched. So, within the first case, we couldn’t get the rest.

$ grep –r –come with=”*txt” “ok” /house/linux/
$ grep –r –come with=”*txt” “NEW” /house/linux/

Within our 2d case, now we have discovered a unmarried output.

Another trend, “CSS” has been used for recursive seek and were given 3 effects, as demonstrated within the image.

$ grep –r –come with=”*txt” “CSS” /house/linux/


The recursive seek has been demonstrated inside this information the use of the Grep command. The “grep” command has used many flags to paintings otherwise whilst doing a recursive seek, i.e., “-r” and “include”. The educational additionally explains the recursive seek with and with out the listing paths given within the grep command. Thus, this text accommodates a package of easy-to-do examples for its customers to be told recursive seek most simply.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More