To use the grep
command to search for a pattern in only .txt
files when running in recursive mode, you can use the -r
option to search recursively, and the --include
option to specify the *.txt
file pattern.
Here is the basic syntax for using the grep
command to search for a pattern in only .txt
files when running in recursive mode:
grep -r --include='*.txt' pattern directory
The pattern
argument is a regular expression that specifies the text to search for. The directory
argument is the directory to search recursively. The --include='*.txt'
option tells grep
to include only .txt
files in the search.
For example, to search for the string "hello" in all .txt
files in the /var/log
directory and its subdirectories, you can use the following command:
grep -r --include='*.txt' hello /var/log
This will search for the string "hello" in all .txt
files in the /var/log
directory and its subdirectories, and display the lines that contain the string.
You can use other options and arguments to customize the search, such as the -i
option to ignore case, or the -n
option to display the line numbers of the matching lines.