Take the 2-minute tour ×
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.

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
3  
Try typing away, write foomytextbar (followed by Enter) after running grep -r 'mytext'. –  sr_ May 16 '12 at 13:42
2  
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
2  
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
3  
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
2  
@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
show 8 more comments

2 Answers

up vote 14 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
5  
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
add comment

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
add comment

Your Answer

 
discard

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