Search code examples
linuxbashunixxargsgnu-findutils

Unix, search for string in multiple files. ( Case sensitive, and accept if the string is in a string )


I've been using this command:

find /path~ -type f | xargs grep -iR STRING1

to find strings in multiple files, but i was wondering me how can i find a string in multiple files, Case Sensitive, and even if the string is in other string.

For example:

I'm searching for: Encoder

  • if a file contains: abcdEncoder — should appear
  • if a file contains: abcdencoder — shouldn't appear
  • if a file contains: encoderEncoder — should appear

Maybe the question is a duplicate, but i haven't find it!


Solution

  • Remove the -i switch to make the matches case-sensitive. Your command already searches multiple files and doesn't care whether the string is inside another string, so that'll give you what you want.

    Also note that using both find -type f and -R is redundant: as -type f ensures find will only print normal files for grep to examine, the -R (recurse through directories) option won't change anything. Alternatively, you can use -R to get rid of find and xargs: grep -R STRING1 /path~