up vote 4 down vote favorite
share [g+] share [fb]

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..

Would someone educate me so I won't embarass myself again? Feel free to move this to another sibling site, if needed..

link|improve this question
6  
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
4  
I agree with MSalters; bash-scripting should have its legitimate place on stackoverflow imho (instead of being constantly migrated to superuser.com)... – ChristopheD Mar 9 '10 at 11:00
If its closed, I'll vote to re-open. Pipes and redirection are essential for bash programming, and this is a very useful (and amusing) corner case that may help someone else. – Tim Post Mar 9 '10 at 11:02
Thanks for all answers, helps a lot with the insight behind how things works instead of stiff manuals. That's what SO is for me, agreeing with @Tim Post. – chelmertz Mar 9 '10 at 11:05
show 1 more comment
feedback

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

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

3 Answers

up vote 19 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...

link|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
show 3 more comments
feedback

And what is the output without > module.txt.

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

link|improve this answer
feedback
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
link|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
feedback

Your Answer

 
or
required, but never shown

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