5

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

ZillGate
  • 153
  • 1
    because you are trying to fetch the word load from all the files inside current directory(recursively) – Avinash Raj May 05 '14 at 18:19
  • @AvinashRaj So why redirection not work? – ZillGate May 05 '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 05 '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"? – phemmer May 05 '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 05 '14 at 18:33
  • @danielkullmann Yes. Your comments are correct! Thanks. – ZillGate May 05 '14 at 18:36

1 Answers1

4

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>.
cuonglm
  • 153,898