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

Tags
shell scripts

 
Thread Tools Search this Thread
# 1  
Old 05-14-2012
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 .Smilie

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

# 3  
Old 05-15-2012
@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
Bug try this one.....

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

# 5  
Old 05-15-2012
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 11:56 AM..

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Search files in directory for keywords using bash cmccabe Shell Programming and Scripting 7 01-11-2016 05:02 PM
Recursively search the string from a column in no. of files millan UNIX for Advanced & Expert Users 3 05-22-2013 10:06 AM
Search files recursively ravisingh Linux 1 03-08-2013 11:44 AM
Perl - use search keywords from array and search a file and print 3rd field when matched chidori Shell Programming and Scripting 11 12-14-2012 01:37 PM
search between keywords and make a single line giri4332 Shell Programming and Scripting 2 10-30-2012 11:56 PM
Need to search for keywords within files modified at a certain time virtual123 UNIX for Advanced & Expert Users 10 09-20-2012 11:51 AM
sed -s does not search recursively hollingv Shell Programming and Scripting 1 04-26-2012 07:09 AM
Search a file with keywords mailabdulbari Shell Programming and Scripting 7 03-10-2010 11:52 PM
How to recursively search and destroy tabs siegfried Shell Programming and Scripting 7 09-30-2009 06:55 PM
Linux shortcutkey to search specific file from a list of directory?! patrick87 UNIX for Dummies Questions & Answers 7 09-07-2009 03:50 AM
sed search and replace recursively 2bone Shell Programming and Scripting 1 09-02-2009 08:05 PM
How to search for keywords in subsequent lines rdhanek Shell Programming and Scripting 12 08-11-2009 06:43 AM
Recursively search for most recent modification lokisapocalypse Shell Programming and Scripting 2 04-17-2009 03:20 PM
Search and replace words between two keywords vrrajeeb Shell Programming and Scripting 6 05-11-2005 01:15 PM