Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search dir for string and print file and line number

Status
Not open for further replies.
Oct 6, 2002
60
US
Hi experts,

Does anyone have a small kornshell script or the appropriate command to search a dir and all sub dirs for a certain string and return the file name and the line number where a string is found? Any help would be greately appreciated

Ryan
 
man find
man xargs
man grep

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
## Replace $dir with the directory you want to search
## i.e . or / or ${HOME}
##
## replace both grep "string"s with the string you
## are searching for
##
for file in $(find dir -exec grep -li string {} \;)
do
echo "FILE:$file\n________________\n"
grep -n string $file
done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top