Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm a little new on searching via bash, so feel free to give me suggestions on the methods to use instead of this, which I'll never use again.

I'm searching for occurances of a string, recursively in a directory, with ~50 not-that-large php-files in it; some in current directory, some in directories beneath current dir, three levels of directories down at most.

The method I'm using is

find . | xargs grep "module" > module.txt

When in simple (one level) directories, this works fine, but in this case, the file became 4 GB large until it filled up all space on the partition. It wasn't even done yet.

Why is that file growing so large?

share|improve this question

migrated from stackoverflow.com Mar 10 '10 at 4:43

This question came from our site for professional and enthusiast programmers.

8  
To understand recursion is to understand recursion –  Tim Post Mar 9 '10 at 10:56
7  
Why close? It's essentially a case of unintended recursion. As a concept, it certainly is programming-related. –  MSalters Mar 9 '10 at 10:57
    
Shell interaction seem to be a tricky issue for site selection. On one hand shells are programs you use, on the other hand they are Turing complete languages in which one writes programs. So site selection depends on a judgment call about whether you are "programming" or not. This is a one-liner which I would have introduced on Super User in the first place, but it is enough of a programmer's problem that I wouldn't vote to move it. –  dmckee Mar 9 '10 at 16:51

3 Answers 3

up vote 20 down vote accepted

module.txt is created before the pipeline starts, therefore it is included in the search. grep finds an instance of "module" in it, so a line is added to it containing the word "module". Which grep then finds, and adds. Which grep then finds, and adds. Which grep then finds, and adds. Which...

share|improve this answer
1  
+1 good one.... –  user22644 Mar 9 '10 at 10:52
    
Nasty. Ok, that's the problem, could you point me to a solution? I.e. with another syntax or just something to add to this search? –  chelmertz Mar 9 '10 at 10:54
2  
@OP: Solution is to ensure your result file name does not contain the search keyword. –  user22644 Mar 9 '10 at 10:55
6  
Store the file outside the search parameters. –  Ignacio Vazquez-Abrams Mar 9 '10 at 10:56
    
@coddadict: of course, +1. Would this be faster or slower than what @MBO suggested? –  chelmertz Mar 9 '10 at 10:57

And what is the output without > module.txt.

Why not try to use grep -R "module" . > ../module.txt?

share|improve this answer
find /path -type f -iname "*.php" | while read -r FILE
do
  grep -H "module" "$FILE" >> "file_with_search_term_found.txt"
done

Or if your grep has recursive function,

grep -RH "module" *.php

Or with bash 4.0 shell

shopt -s globstar
for file in /path/**/*.php
do
   if [ -f "$file" ];then
      while read -r line 
      do
         case "$line" in
           *module* ) echo $line >> module.txt;;
         esac    
      done <"file"      
   fi

done
share|improve this answer
    
Or find -name '*.php' -exec grep -q -H "module" {} \; -print > ../module.txt. –  Ignacio Vazquez-Abrams Mar 9 '10 at 11:11
    
i prefer find /path -type f -iname "*.php" -exec grep -q -H "module" "{}" +; ... –  user31894 Mar 9 '10 at 12:00

Your Answer

 
discard

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.