
The -R is dereferenced search which means it will follow the symbolic links to go to the original file (which may be located in some other part of the system). So, what's the difference grep -r and grep -R? Only one, actually. There is also a -R option for recursive search and it works almost the same as the -r option. Here's the result: Recursive search with -r option of grep command Here's the recursive search I performed in the previous example to do a grep search in the current folder: grep -r simple. With this option, grep will look into all the files in the current (or specified) directory and it will also look into all the files of all the subdirectories. Grep provides a -r option for the recursive search. Grep recursive search in all subdirectories of a directory Now that you know that, let's see how you can perform a recursive search with grep so that it also looks into the files in the subdirectories. If you are not in the same directory where you want to perform, you can specify the directory path and end it with /* grep search_term directory_path/*īasically, you are using the wild card to expand on all the elements (files and directories) of the given directory. Search in all files of a directory with grep Since you cannot directly grep search on a directory, it will show "XYZ is a directory" error along with search results. This will search in all the files in the current directories, but it won't enter the subdirectories. The wild card actually substitutes with the name of all the files and directories in the current directory. To search for the word 'simple' in all the files of the current directories, just use wild card (*). Except empty.txt, all files contain the term 'simple' on which I'll perform the grep search. Here's the directory structure I am going to use in this example. Let me show you all this in details with proper examples so that it is easier for you to understand. You may also specify the directory path if you are not in the directory where you want to perform the search: grep -r search_term directory_path You can make grep search in all the files and all the subdirectories of the current directory using the -r recursive search option: grep -r search_term. It only searches in all the files in the current directory. If you want to search all the files in a directory with grep, use it like this: grep search_term * Usually, you run grep on a single file like this: grep search_term filename logsearch.sh localhost /var/tmp/logXray autonda /var/log 60m 'fatal_P_error_P_critical_P_failure_P_warning' '.Grep is an excellent tool when you have to search on the content of a file. Solution to the Original Poster's Issue: Scan for Multiple strings in multiple log files. logsearch.sh localhost /var/tmp/logXray autonda /var/log/messages 60m 'can.*t.*open_P_ntpd.*stat' '.' 1 2 blahblahA -ndfoundmul When using '-ndfoundmul', you'll get an output similar to. If you just want to see the total count of each pattern found, simply replace '-ndshow' with '-ndfoundmul'. The log option -ndshow - This is the parameter you want to use if you wish to output the entries from the logs found matching the pattern(s) you specified.It records stats about the log file(s) you're monitoring under /var/tmp/logXray The tag - this is the second to last argument you have to supply.The strings(s)/pattern(s) you want to watch for.do not monitor or discover any log file that has a timestamp over 60 minutes The age a log file must be for it to be monitored.i.e.


logsearch.sh localhost /var/tmp/logXray autonda /var/log/messages 60m 'can.*t.*open_P_ntpd.*stat' '.' 1 2 multi_errCheck -ndshow Scenario 2: Monitor MULTIPLE strings in just ONE log file. logsearch.sh localhost /var/tmp/logXray autonda /var/log/messages 60m 'can.*t.*open' '.' 1 2 single_errCheck -ndshow Scenario 1: Monitor ONE string in just ONE log file. Nevertheless, it is done and ready and can be downloaded from the following link: But I recently had to do this and it was quite painful.

And yes, it certainly needed to be scripted if you're going to search for multiple strings in multiple different logs at the same time.
