Ubuntu – Search for a string recursively whilst in current directory

directorygrep

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?

Best Answer

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.