Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to recursively search for a list of keywords in a given directory?

👤 Login to reply

    #1  
Old 05-14-2012
neelmani neelmani is offline
Registered User
 
How to recursively search for a list of keywords in a given directory?

Hi all,
how to recursively search for a list of keywords in a given directory??

for example:
suppose i have kept all the keywords in a file called "procnamelist" (in separate line)
and i have to search recursively in a directory called "target/dir"

if i am not doing recursive search then the below command is working.
Code:
fgrep -if  procnamelist target/dir/*

but when i am using the below command for the recursive search, its throwing error as "bad option -r"
Code:
fgrep -rif  procnamelist target/dir/*

or
Code:
fgrep -Rif  procnamelist target/dir/*



not working.

please help .

Last edited by Franklin52; 05-14-2012 at 04:24 PM.. Reason: Please use code tags for data and code samples, thank you
Sponsored Links
    #2  
Old 05-14-2012
glev2005 glev2005 is offline
Registered User
 
Perhaps:
Code:
cat procnamelist|while read line;do fgrep -Rif $line target/dir/*;done

Sponsored Links
    #3  
Old 05-15-2012
neelmani neelmani is offline
Registered User
 
@glev2005 not working i am getting same error as :fgrep: illegal option -- R
actually the r option is not there in fgrep.. any other way??
    #4  
Old 05-15-2012
pamu pamu is offline
Registered User
 
Bug try this one.....

Code:
while true
do
while read line
do
cat file | grep "$line" 
done<procnamelist
done

Sponsored Links
    #5  
Old 05-15-2012
alister alister is offline
Registered User
 
Whenever you need to traverse the a filesystem but the command does not support -R/r, think find(1).
Code:
find target/dir -type f -exec grep -iFf procnamelist /dev/null {} \;

If your find is compliant with the POSIX 2004 edition or later, use + to increase efficiency:
Code:
find target/dir -type f -exec grep -iFf procnamelist /dev/null {} +



Quote:
Originally Posted by glev2005
Code:
cat procnamelist|while read line;do fgrep -Rif $line target/dir/*;done

That would have failed even if -[r|R] were supported. A line from procnamelist, which is a pattern to match, is treated as a filename argument to -f. If you put procnamelist where it belongs, after -f, the pipe and while loop serve no purpose.


Quote:
Originally Posted by pamu
Code:
while true
do
while read line
do
cat file | grep "$line" 
done<procnamelist
done

The sole reason for the existence of this thread is file system traversal. This proposal is an infinite loop that reads one file over and over and over and ...

Regards,
Alister

Last edited by alister; 05-15-2012 at 10:56 AM..
Sponsored Links
👤 Login to reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search a file with keywords mailabdulbari Shell Programming and Scripting 7 03-10-2010 10:52 PM
Linux shortcutkey to search specific file from a list of directory?! patrick87 UNIX for Dummies Questions & Answers 7 09-07-2009 02:50 AM
How to search for keywords in subsequent lines rdhanek Shell Programming and Scripting 12 08-11-2009 05:43 AM
Search and replace words between two keywords vrrajeeb Shell Programming and Scripting 6 05-11-2005 12:15 PM



All times are GMT -4. The time now is 07:41 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?