I want to search all files recursively from the directory I am in for a particular string.

I tried

grep -r -i my_string

and I tried it in a test folder with two tiny files but it wouldn't find the string.

Also, is there any special way of defining "contains" rather than matching the whole word?

  • 4
    Try grep -r -o -i "your_string" *(with " quotes). – pl1nk Jun 2 '12 at 1:31
  • i am getting a few permission denied messsages, does it just ignore those files but continue checking the rest? – David Jun 2 '12 at 2:40
  • Right, these files are getting ignored. – pl1nk Jun 2 '12 at 12:15

Are you searching for a file name or a word inside a file? grep searches inside the files, which seems to be what you want.

grep uses regular expressions - in fact, that's what the "re" part stands for. So, it does not use the normal bash wildcards; that is, abc* would not find words starting with abc, it would find words starting with ab, then zero or more c's following. But yes, if you simply give it a pattern, it will find it anywhere, with no knowledge of whole words. If you wanted to find whole words, you would need to create a regular expression to tell it that.

But, like most commands, you need to put the filename(s) at the end of the command, so your example would just sit there and wait for the filename.

As @plink said, use

grep -r -o -i "your_string" *

(or *.*, or other filespec). The errors can be ignored. If you are getting a lot of errors, you can append 2>/dev/null to the end, which will redirect stderr to a null device.

  • 6
    For those curious: -r means recursive (i.e. it looks in the current folder and ALL folders in the current folder AND all folders in those folder.....till infinity), -o means it doesn't show whole the line, and -i is ignore case. – BKSpurgeon Dec 31 '16 at 2:42
  • @johnsnails The backtick isn't necessary. It's more likely to be a formatting error on Stack Exchange when posting as inline code block. – tom_mai78101 Aug 13 '19 at 12:25
  • Yeah that makes sense. – johnsnails Aug 14 '19 at 9:46
  • 1
    The command can be shortened to grep -ior "your string" * – kas Jul 3 '20 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.