How to find files containing specific text in Linux?

· · 4139 views

I'm trying to figure out how to scan my whole Linux system for all files containing a particular string of text.

This ability to discover text strings in files would be very helpful for some programming projects I'm working on.

0
2 Answers

You can use grep -ilR:

grep -Ril "text-to-find-here" /directory-path
  • i stands for ignore case (optional in your case).
  • R stands for recursive.
  • l stands for "show the file name, not the result itself".
0

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

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

Please login or create new account to participate in this conversation.