“Grep” is a very useful command of Bash to search the content in a file. The regular expression pattern can be used with the “grep” command to search for any specific text in a file. The normal “grep” command is used to search the content in a single file but sometimes it requires searching the content in multiple files that are stored in a folder. The recursive “grep” command is used to search the content in a directory of multiple files. The methods of using the recursive “grep” in Bash are shown in this tutorial.
Syntax of Recursive Grep:
Here, the -r option is used with the “grep” command to search recursively. The <pattern> is used to search the content in the file of the particular directories including the sub-directories. The <directory> is used to define the directory that may contain one or more files and directories. And the <pattern> is searched in these files and the files of the sub-directories.
Example 1: Simple Use of “Grep”
The <pattern> of the “grep” command can be a simple string or a regular expression pattern. Run the following commands to check the content of the “sales.txt” file and search the “Feb” string in the file of the current directory:
$ grep 'Feb' *
According to the following output, the “Feb” string exists in the second line of the “sales.txt” file. The output of the “grep” command shows that the search string is found in the “sales.txt” file.
Example 2: Using the “Grep” Command with -R Option and –Exclude-Dir Flag
Run the following command to search the “CSE” string recursively by excluding the temp directory:
According to the following output, the “CSE” string exists in the three lines of the “sales.txt” file of the temp folder:
Example 3: Using the “Grep” Command with -R Option
The -R option is used with the “grep” command to search the content in the file and the symbolic link of the file. Run the following command to search the “sleep” string in all files and the symbolic links of the files of the current directories and sub-directories:
The following output shows the search result of the “sleep” string in the current directory. One file of a symbolic link named “sleeplink.bash” is contained in the output. The other three files are “sleep1.bash”, “sleep2.bash”, and “sleep3.bas”:
Example 4: Using the “Grep” Command with -I Option
The “grep” command searches the string value in a case-sensitive manner. The -i option is used with the “grep” command to search the content in the file in a case-insensitive manner. Run the following command to search the “Credit” string in all files of the current directories and sub-directories:
According to the output, the “credit” string exists in two locations of the “sum1.bash” file. The searching string is “Credit” and it matches with the “credit” string for using the -i option:
Example 5: Using the “Grep” Command with the “^” Symbol
The “^” symbol is used to search the string at the beginning of the line of a file. Run the following command that searches the “printf” string in all Bash files of the current directory:
According to the output, the “printf” string exists in one location of the “namedarg3.bash” file:
Example 6: Using the “Grep” Command with the “$” Symbol
The “$” symbol is used to search the string at the end of the line of a file. Run the following command that searches the lines in all text files of the current directory that contain the digits at the end of the file:
According to the output, three files contain numeric values at the end of the lines. These are “courses.txt”, “employees.txt”, and “sales.txt”:
Example 7: Using the “Grep” Command with [0-9] Range Class
The [0-9] range is used to define all numbers that contain any digit from 0 to 9. Run the following command that searches the lines in all text files of the current directory that contain a “CSE” string and any digit in the files of the current directory:
According to the output, a file named “courses.txt” of the current directory contains “CSE” with the digits in three lines:
Conclusion
The “grep” command is mainly used to search in a particular file but the recursive “grep” is used to recursively search in multiple files of a directory. So, the recursive “grep” is more efficient for deep searching. The uses of the recursive “grep” using different types of options and patterns are shown in this tutorial.