Recursive Grep Tutorial

Grep is a command-line utility in Linux that can be used for printing lines that match a pattern.

Mainly grep can be used to search any string in all files available in the directory hierarchy and writes each matching line to standard output.

In this tutorial, we will show you how to search a pattern or string Recursively in all files available in a specific directory and its sub-directories.

Basic Syntax

The basic syntax of the grep command is shown below:

grep [OPTIONS] PATTERN [FILE...]

Or

grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

Where:

  • OPTIONS : This is a number of options available in grep.
  • PATTERN : This is specified as a regular expression.
  • FILE : This can be used to obtain patterns from FILE.
  • -e : This can be used to specify multiple search patterns.

Search Pattern Recursively in Files

Use the following syntax to find the name of files with path recursively under specific directory and sub-directories that contains the particular string:

grep -iR "search-pattern" /path-of-the-directory

Where :

  • -i : This option ignores, case for a matching pattern.
  • -R : This option read all files under each directory, recursively.

Let’s see a simple example to search the pattern “virtualhost” in all files available under /etc/apache2 directory:

grep -iR "virtualhost" /etc/apache2/

You should see the following output:


/etc/apache2/conf-enabled/localized-error-pages.conf:# even on a per-VirtualHost basis. If you include the Alias in the global server
/etc/apache2/conf-enabled/other-vhosts-access-log.conf:# Define an access log for VirtualHosts that don't define their own logfile
/etc/apache2/ports.conf:# have to change the VirtualHost statement in
/etc/apache2/conf-available/localized-error-pages.conf:# even on a per-VirtualHost basis. If you include the Alias in the global server
/etc/apache2/conf-available/other-vhosts-access-log.conf:# Define an access log for VirtualHosts that don't define their own logfile
/etc/apache2/apache2.conf:# If you do not specify an ErrorLog directive within a <VirtualHost>
/etc/apache2/apache2.conf:# logged here. If you *do* define an error logfile for a <VirtualHost>
/etc/apache2/sites-available/default-ssl.conf: <VirtualHost _default_:443>
/etc/apache2/sites-available/default-ssl.conf: </VirtualHost>
/etc/apache2/sites-available/000-default.conf:<VirtualHost 127.0.0.1:8080>
/etc/apache2/sites-available/000-default.conf:</VirtualHost>

You can also make you search more specific using –include option to search a string in all files with specific extension only and ignore others.

For example, search a string “virtualhost” in all files under /etc/apache2 directory with the extension .conf as shown below:

grep -iR --include="*.conf" "virtualhost" /etc/apache2/

You can also use multiple file extensions with the following command:

grep -iR --include="*.conf" --include="*.php" "virtualhost" /etc/apache2/

You can use -n option with grep to show the line number for a matching pattern also:

grep -inR --include="*.conf" "virtualhost" /etc/apache2/

You should see the following output:

/etc/apache2/conf-enabled/localized-error-pages.conf:27:# even on a per-VirtualHost basis. If you include the Alias in the global server
/etc/apache2/conf-enabled/other-vhosts-access-log.conf:1:# Define an access log for VirtualHosts that don't define their own logfile
/etc/apache2/ports.conf:2:# have to change the VirtualHost statement in
/etc/apache2/conf-available/localized-error-pages.conf:27:# even on a per-VirtualHost basis. If you include the Alias in the global server
/etc/apache2/conf-available/other-vhosts-access-log.conf:1:# Define an access log for VirtualHosts that don't define their own logfile
/etc/apache2/apache2.conf:123:# If you do not specify an ErrorLog directive within a <VirtualHost>
/etc/apache2/apache2.conf:125:# logged here. If you *do* define an error logfile for a <VirtualHost>>
/etc/apache2/sites-available/default-ssl.conf:2: <VirtualHost _default_:443>
/etc/apache2/sites-available/default-ssl.conf:133: </VirtualHost>
/etc/apache2/sites-available/000-default.conf:1:<VirtualHost 127.0.0.1:8080>
/etc/apache2/sites-available/000-default.conf:29:</VirtualHost>

Recursive Grep with Find

You can also search a directory and sub-directories for files that match a pattern by combining the find command with grep.

For example, search a string “virtualhost” in all files under /etc/apache2 directory and all sub-directories with the following command:

find /etc/apache2/ -type f -exec grep -il 'virtualhost' {} \;

You should see the following output:

/etc/apache2/ports.conf
/etc/apache2/conf-available/localized-error-pages.conf
/etc/apache2/conf-available/other-vhosts-access-log.conf
/etc/apache2/apache2.conf
/etc/apache2/sites-available/default-ssl.conf
/etc/apache2/sites-available/000-default.conf

Search Recursively with Egrep

You can also search a directory and sub-directories for files that match a pattern with the egrep command.

egrep is mainly used to search for multiple patterns at one time.

For example, search a string “virtualhost” and “www.example.com” recursively in all files under /etc/apache2 directory and all sub-directories as shown below:

egrep -iR "virtualhost|www.example.com" /etc/apache2/

You should see the following output:

/etc/apache2/conf-enabled/localized-error-pages.conf:#ErrorDocument 402 http://www.example.com/subscription_info.html
/etc/apache2/conf-enabled/localized-error-pages.conf:# even on a per-VirtualHost basis. If you include the Alias in the global server
/etc/apache2/conf-enabled/other-vhosts-access-log.conf:# Define an access log for VirtualHosts that don't define their own logfile
/etc/apache2/ports.conf:# have to change the VirtualHost statement in
/etc/apache2/conf-available/localized-error-pages.conf:#ErrorDocument 402 http://www.example.com/subscription_info.html
/etc/apache2/conf-available/localized-error-pages.conf:# even on a per-VirtualHost basis. If you include the Alias in the global server
/etc/apache2/conf-available/other-vhosts-access-log.conf:# Define an access log for VirtualHosts that don't define their own logfile
/etc/apache2/apache2.conf:# If you do not specify an ErrorLog directive within a <VirtualHost>
/etc/apache2/apache2.conf:# logged here. If you *do* define an error logfile for a <VirtualHost>
/etc/apache2/sites-available/default-ssl.conf: <VirtualHost _default_:443>
/etc/apache2/sites-available/default-ssl.conf: </VirtualHost>
/etc/apache2/sites-available/000-default.conf:<VirtualHost 127.0.0.1:8080>
/etc/apache2/sites-available/000-default.conf: #ServerName www.example.com
/etc/apache2/sites-available/000-default.conf:</VirtualHost>

Conclusion

The grep command allows you to search a string recursively in all files under specific directory and sub-directories. For more information, you can see grep documentation at grep doc.

We hope this Recursive Grep Tutorial was useful and if you have any questions, please use the comments below to ask them!