HowTo: Use Grep Command In Unix / Linux To Search Subdirectories

To use the grep command to search for a pattern in all subdirectories of a directory in Unix or Linux, you can use the -r or --recursive option, followed by the directory path, the pattern you want to search for, and the file types you want to search in (if any).

Here’s an example of using the grep command to search for the word “error” in all .log files under the /var/log directory:

grep -r "error" /var/log/ --include=*.log

This will search for the pattern “error” in all files with the .log extension in the /var/log directory and its subdirectories. The output will show the matching lines and their file names.

Leave a Comment