How to grep All Sub-Directories for Files

How to grep All Sub-Directories for Files
How to grep All Sub-Directories for Files

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.

"How

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:

"How

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:

"How

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here