The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com




Thread: Recursive grep
View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 05-16-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The relative path to file2 seems wrong; the output redirection is relative to the current directory, not the directory of the file you are grepping.

The relative pat you are grepping seems wrong too; /../ is equivalent to / is equivalent to /../../../../../

The backticks in the for loop are what are splitting up stuff on whitespace. Use a construct which is less sensitive to spacing issues, or use proper quoting.

Code:
for h in "`cat file1`"; do grep -rl "$h" pathtodir >>file2; done
or

Code:
while read h; do grep -rl "$h" pathtodir >>file2; done<file1