HomeLinuxMethods to Use the Recursive Grep Command to Search within the Listing

Methods to Use the Recursive Grep Command to Search within the Listing


“Grep” is a really helpful command of Bash to look the content material in a file. The common expression sample can be utilized with the “grep” command to seek for any particular textual content in a file. The traditional “grep” command is used to look the content material in a single file however typically it requires looking the content material in a number of information which might be saved in a folder. The recursive “grep” command is used to look the content material in a listing of a number of information. The strategies of utilizing the recursive “grep” in Bash are proven on this tutorial.

Syntax of Recursive Grep:

grep -r <sample> <listing>

Right here, the -r possibility is used with the “grep” command to look recursively. The <sample> is used to look the content material within the file of the actual directories together with the sub-directories. The <listing> is used to outline the listing that will include a number of information and directories. And the <sample> is searched in these information and the information of the sub-directories.

Instance 1: Easy Use of “Grep”

The <sample> of the “grep” command could be a easy string or an everyday expression sample. Run the next instructions to examine the content material of the “gross sales.txt” file and search the “Feb” string within the file of the present listing:

$ cat gross sales.txt

$ grep ‘Feb’ *

In accordance with the next output, the “Feb” string exists within the second line of the “gross sales.txt” file. The output of the “grep” command exhibits that the search string is discovered within the “gross sales.txt” file.

Instance 2: Utilizing the “Grep” Command with -R Choice and –Exclude-Dir Flag

Run the next command to look the “CSE” string recursively by excluding the temp listing:

$ grep -r ‘CSE*’ –exclude-dir= “temp”

In accordance with the next output, the “CSE” string exists within the three strains of the “gross sales.txt” file of the temp folder:

p2

Instance 3: Utilizing the “Grep” Command with -R Choice

The -R possibility is used with the “grep” command to look the content material within the file and the symbolic hyperlink of the file. Run the next command to look the “sleep” string in all information and the symbolic hyperlinks of the information of the present directories and sub-directories:

The next output exhibits the search results of the “sleep” string within the present listing. One file of a symbolic hyperlink named “sleeplink.bash” is contained within the output. The opposite three information are “sleep1.bash”, “sleep2.bash”, and “sleep3.bas”:

p3

Instance 4: Utilizing the “Grep” Command with -I Choice

The “grep” command searches the string worth in a case-sensitive method. The -i possibility is used with the “grep” command to look the content material within the file in a case-insensitive method. Run the next command to look the “Credit score” string in all information of the present directories and sub-directories:

In accordance with the output, the “credit score” string exists in two places of the “sum1.bash” file. The looking string is “Credit score” and it matches with the “credit score” string for utilizing the -i possibility:

Instance 5: Utilizing the “Grep” Command with the “^” Image

The “^” image is used to look the string in the beginning of the road of a file. Run the next command that searches the “printf” string in all Bash information of the present listing:

In accordance with the output, the “printf” string exists in a single location of the “namedarg3.bash” file:

Instance 6: Utilizing the “Grep” Command with the “$” Image

The “$” image is used to look the string on the finish of the road of a file. Run the next command that searches the strains in all textual content information of the present listing that include the digits on the finish of the file:

In accordance with the output, three information include numeric values on the finish of the strains. These are “programs.txt”, “workers.txt”, and “gross sales.txt”:

Instance 7: Utilizing the “Grep” Command with [0-9] Vary Class

The [0-9] vary is used to outline all numbers that include any digit from 0 to 9. Run the next command that searches the strains in all textual content information of the present listing that include a “CSE” string and any digit within the information of the present listing:

In accordance with the output, a file named “programs.txt” of the present listing comprises “CSE” with the digits in three strains:

Conclusion

The “grep” command is principally used to look in a selected file however the recursive “grep” is used to recursively search in a number of information of a listing. So, the recursive “grep” is extra environment friendly for deep looking. The makes use of of the recursive “grep” utilizing various kinds of choices and patterns are proven on this tutorial.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments