To use the grep
command to search for a pattern in subdirectories in Unix or Linux, you can use the -r
or --recursive
option.
For example, to search for the pattern "test" in all files in the current directory and its subdirectories, you can use the following command:
grep -r "test" .
This will search for the pattern "test" in all files in the current directory and its subdirectories, and print the name of each file that contains the pattern along with the matching lines.
You can also use the -H
or --with-filename
option to print the name of the file before each matching line. For example:
grep -rH "test" .
You can also use the -n
or --line-number
option to print the line number before each matching line. For example:
grep -rHn "test" .
You can also use the -l
or --files-with-matches
option to print only the names of the files that contain the pattern, without printing the matching lines. For example:
grep -rl "test" .