Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

I found if I search using grep without specifying a path, like grep -r 'mytext' it takes infinitely long. Meanwhile if I search with path specified grep -r 'mytext' . it instantly finds what I need. So, I'm curious, in first form, in which directory does grep search?

UDATE: grep version: grep (GNU grep) 2.10

share|improve this question
Try typing away, write foomytextbar (followed by Enter) after running grep -r 'mytext'. – sr_ May 16 '12 at 13:42
Maybe you should specify what king of grep is that. With the GNU grep I use on Linux grep -r 'mytext' and grep -r 'mytext' . does the same thing: search recursively starting from the current directory. When the -r switch is used GNU grep not searches STDIN. – manatwork May 16 '12 at 14:12
Ah. I tested with grep 2.12: pastebin.com/L4my26QS In meantime I found this in the NEWS file regarding version 2.11 (2012-03-02): "If no file operand is given, and a command-line -r or equivalent option is given, grep now searches the working directory. Formerly grep ignored the -r and searched standard input nonrecursively. An -r found in GREP_OPTIONS does not have this new effect." – manatwork May 16 '12 at 15:06
GNU grep recently changed behavior with -r argument (see the commit). So "grep -r PATTERN" with newer grep will search current directory and not STDIN. – hluk May 16 '12 at 15:08
@Mikel, i updated my question with a grep version. It seems that answer I pointed as correct is indeed a correct, because I'm using an 'old' grep. So it doesn't matter do I specify -r or not, if no directory is provided, it searches in STDIN. Btw, thanks for that investigation, now, I'll be aware of future changes in grep ;) – user7477 May 17 '12 at 5:48
up vote 16 down vote accepted

Actually it doesn't search anywhere. It waits for input from standard input.

Try this:

beast:~ viroos$ grep foo

when you type line containing "foo" and hit enter this line will be repeated otherwise cursor will be moved to new line but grep won't print anything.

share|improve this answer
As I commented above grep -r PATTERN with newer version of GNU grep will not read standard input but search for pattern in current directory (see the commit). This change makes sense since you probably don't want to search standard input when you specify -r flag. – hluk May 16 '12 at 15:13
I checked my grep version, it's 2.10, so this answer is perfectly relevant. Btw, now, i'll know what to wait from newer versions of grep. – user7477 May 17 '12 at 5:49

Exactly as the previous answer by Maciek says:

Actually it doesn't search anywhere. It waits for input from standard input.

However you can write into the stdin of an already running program if you have /proc available by piping data into it's 0th first descriptor:

In one terminal run

grep "hello"

and see it wait indefinitely...In another terminal type this:

echo "hello $USER" > /proc/`pgrep grep`/fd/0

see the first terminal print hello and your username.

share|improve this answer

Your Answer


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