How to use “grep” command to find text including subdirectories

How to use “grep” command to find text including subdirectories

I want to find all files which contain a specific string of text.

grep -rlHn "string" /path

• -r (or –recursive) option is used to traverse also all sub-directories of /path, whereas
• -l (or –files-with-matches) option is used to only print filenames of matching files, and not the matching lines (this could also improve the speed, given that grep stop reading a file at first match with this option).
• -H causes the filename to be printed (implied when multiple files are searched)
• -n causes the line number to be printed

Example

Find the file that has class “signle-product” in the web folder.

grep -rlHn "woocommerce-product-gallery__wrapper" /var/www/html

Result

/var/www/html/includes/single-product.php

,