How to grep All Sub-Directories for Files

Table of Contents
grep All Sub Directories for Files
The grep stands for “Global Regular Expression Print”. The grep command used to find a particular string or pattern in one or multiple files. In this tutorial, you are going to learn How to grep All Sub Directories for Files.
Basic Syntax
Following is the basic syntax to grep all Subdirectories for Files:
grep [OPTIONS] [WORD_OR_STRING_PATTERN] [FILES...]
1. Search All Subdirectories For Files in Current Directory
For searching all subdirectories recursively -r
or -R
option used with grep
command. If you are going to search in the current working directory then you need not specify the directory.
In the following example, we are going to search the word error
inside current directories recursively:
grep -r 'error'
or you can also use -R option with it
grep -R 'error'
2. Search All Subdirectories For Files in Specific Directory
For searching all subdirectories recursively the -r
option used with grep
command.
In the following example, we are going to search error
word inside var/logs
directories recursively:
grep -r 'error' /var/log
3. grep All Subdirectories For Files with Color Output
You can get all output in color by using --color
option with grep command.
grep -r --color 'server' /etc/*.conf
In the above example, the word server
is searched in etc
directories all configuration files.
3. List All Subdirectories For Files
To list all subdirectories for files with matching word or string, the -l
option used with grep
command:
grep -r -l 'server' /etc/*.conf
The output should be:
4. grep All Subdirectories For Files Case-Insensitively
To list all subdirectories for files case-insensitively the -i
option is used with grep
command:
grep -r -i 'server' /etc/*.conf
The output should be:
Conclusion
You have successfully learned how to grep All Sub Directories for Files. If you have any queries regarding this then please don’t forget to comment below.
LATEST POSTS
-
How to Install Gradle on Debian 10
-
How to Install Postman on Debian 9
-
How to Install Sublime Text 3 on Debian 10
-
How to Set Up Cron Job on CentOS 8
-
How to Install R on Debian 10
-
How to Add Swap Space on Debian 10
-
How to Install Google Earth on Debian 9
-
How Install Redis on CentOS 8
-
How to Install Yarn on CentOS 7
-
How to Install DataGrip on CentOS 8
-
How to Install Composer on CentOS 8
-
How to Install Opera Browser on CentOS 8