Asked by Sumit Talwar · · 2 answers
You can use grep
command for finding a file containing a particular text string.
The syntax is:
grep [option] "text string to search" directory-path
Examples:
1. Search Single String in All Files
grep -rlw "text string to search" /var/log
2. Search Multiple String in All Files
grep -rlw -e "string1" -e "string2" /var/log
3. Search String in Specific Files
grep -rlw --include="*.log" -e "text string to search" /var/log
4. Exclude Some Files from Search
grep -rlw "text string to search" /var/log
grep -rlw -e "string1" -e "string2" /var/log
grep -rlw --include="*.log" -e "text string to search" /var/log
You can exclude some files using –exclude
option in command. For example, do not search file ending with .txt
extension.
grep -rlw --exclude="*.txt" -e "text string to search" /var/log
5. Exclude Some Directories from Search
You can also exclude some directoires to skip search inside it. For example, do not search string files inside any folder having directory-name
in their name.
grep -rlw --exclude-dir="*directory-name*" -e "text string to search" /var/log
0
0