Home Man
Search
Today's Posts
Register

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?

Tags
shell scripts

Login to Reply

 
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 .

Last edited by Franklin52; 05-14-2012 at 04: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 10:56 AM..
Login to Reply

« Previous Thread | Next Thread »
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 files in directory for keywords using bash cmccabe Shell Programming and Scripting 7 01-11-2016 04:02 PM
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 12:37 PM
search between keywords and make a single line giri4332 Shell Programming and Scripting 2 10-30-2012 10:56 PM
Need to search for keywords within files modified at a certain time virtual123 UNIX for Advanced & Expert Users 10 09-20-2012 10:51 AM
sed -s does not search recursively hollingv Shell Programming and Scripting 1 04-26-2012 06:09 AM
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 02:23 PM.

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