Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In a directory I have a file that contains the word string.
When I do: grep "string" . no results are returned.
But when I do grep -r "string" . the file is returned.
Why is -r option needed? It does not look inside internal subdirectories.

share|improve this question
up vote 5 down vote accepted

No it is not mandatory. grep "string" . means look for string in a file called .. If you want to run grep on all files in the current directory, use grep string * and if you want to look in all files in all subdirectories recursively, do grep -r string.

share|improve this answer
But in the version grep -r "string" . the . is interpreted as start from . directory? – Jim Jul 7 '13 at 21:08
You usually want to use grep "string" filename You're giving it '.' which is a directory (the current directory). If you want grep to search in a file, give it a filename, not a directory. – valbaca Jul 7 '13 at 21:12
@Jim I never said anything about grep -r "string" ., I said grep -r string with no . but it doesn't matter, the two are equivalent. Yes, with -r the . (which is implied if absent) is interpreted s start from .. Why is that strange? -r means search through all files in this directory and its children, so it will find the file. With no r, grep will search through the files you give it, if you just give it . it will treat it as a file and attempt to search through it, ignoring any other files you have not mentioned in the command line. – terdon Jul 7 '13 at 21:13
@valbaca:Ok but my question is if the third argument is interpreted as a filename or directory depending on the usage of -r or not – Jim Jul 7 '13 at 21:13
@Jim yes. Unless you run grep -r grep will treat its arguments as files. If you give it a glob (eg *txt) that will be expanded by the shell before grep sees it so again, grep will see a list of files. If run grep -r, it will treat the third argument as a path and search all files in that path. – terdon Jul 7 '13 at 21:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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