4

I am doing a recursive grep by the following command:

grep -r "load" . > tmp.txt

However, when I hit enter, the command does not execute and bash is waiting for more inputs.. My OS is Ubuntu 12.04.

I have checked the following two answers but they could not solve my problem.

Why does grep not work with redirection?

how do i redirect output from tailf & grep to a file

closed as off-topic by Patrick, devnull, slm, derobert, Ramesh May 5 '14 at 20:11

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Patrick, devnull, slm, derobert, Ramesh
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    because you are trying to fetch the word load from all the files inside current directory(recursively) – Avinash Raj May 5 '14 at 18:19
  • @AvinashRaj So why redirection not work? – ZillGate May 5 '14 at 18:20
  • 1
    Have you looked at the size or content of tmp.txt? One reason could be that the output file is also searched by grep, resulting in an infinite amount of results. And how large is the current directory, incl. subdirs? – daniel kullmann May 5 '14 at 18:30
  • 1
    @ZillGate I think what Avinash is trying to say is that the command is probably just taking a while to complete. Why do you say that "bash is waiting for more input"? – Patrick May 5 '14 at 18:30
  • @Patrick Yes. You and Avinash are right. I thought it will be rather quick but apparently I was wrong. Thank you! – ZillGate May 5 '14 at 18:33
3

I also use Ubuntu 12.04, and I get this error:

$ grep -r 'test' . > tmp.txt
grep: input file `./tmp.txt' is also the output

Because redirection will be expand first, so tmp.txt is created in current directory before grep is executed, leading to error occurs.

If I change tmp.txt to other path, like /tmp/tmp.txt, then it works normally.

My grep version:

$ grep --version
grep (GNU grep) 2.10
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
  • Yes. Another cause is that the grep command takes too long. – ZillGate May 5 '14 at 22:02

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