How to grep all subdirectories of a file

How to grep all subdirectories of a file

All subdirectories of grep files

grep stands for “Global Regular Expression Print”. A grep command used to find a specific string or pattern in one or more files. In this tutorial, you will learn how to grep all subdirectories of a file.

Basic syntax

The following is the basic syntax for greping all subdirectories of a file.

grep [OPTIONS] [WORD_OR_STRING_PATTERN] [FILES...]

1. Search all subdirectories of files in current directory

To recursively search all subdirectories -r Or -R Options used in grep command. You do not need to specify a directory when searching in the current working directory.

The following example searches for a word error Recursively in the current directory:

grep -r 'error'

Or you can also use -R option with it

grep -R 'error'

2. Search all subdirectories of files in a specific directory

To search all subdirectories recursively -r Options used in grep command.

The following example searches error Words inside var/logs Recursively directories:

grep -r 'error' /var/log

3. grep all subdirectories of color output files

Can be used to get all output in color --color Options using the grep command.

grep -r --color 'server' /etc/*.conf

In the example above, the word server Will be searched for etc Directory for all configuration files.

How to grep all subdirectories of a file

3. List all subdirectories of a file

To list all subdirectories of a file with a matching word or string, -l Options used in grep command:

grep -r -l 'server' /etc/*.conf

The output looks like this:

How to grep all subdirectories of a file

4.grep all subdirectories of files case insensitive

To list all subdirectories of a file in a case-insensitive manner -i Options are grep command:

grep -r -i 'server' /etc/*.conf

The output looks like this:

How to grep all subdirectories of a file

Conclusion

You learned how to grep all subdirectories of a file. If you have any questions about this, don’t forget to comment below.

Sidebar