Re: [SuSE Linux] grep recursive
I have to search all the files in a directory and its subdirectories and their subdirectories for a string.
I can search a directory at a time with fgrep 'string' *.*, but this is >taking ages to do for every dirictory.
Is their a way to grep recursively through the directories? I could not find anything like that in the manual.
Thanks >Nico -
A friend of mine has a utility called "tgrep" which does exactly what you describe. Has anyone else heard of this? If it's not available out there I could see about getting it to you. Gordy gordy@usjet.net ___________________________________________________________________ You don't need to buy Internet access to use free Internet e-mail. Get completely free e-mail from Juno at <A HREF="http://www.juno.com/getjuno.html"><A HREF="http://www.juno.com/getjuno.html</A">http://www.juno.com/getjuno.html</A</A>> or call Juno at (800) 654-JUNO [654-5866] - To get out of this list, please send email to majordomo@suse.com with this text in its body: unsubscribe suse-linux-e Check out the SuSE-FAQ at <A HREF="http://www.suse.com/Support/Doku/FAQ/"><A HREF="http://www.suse.com/Support/Doku/FAQ/</A">http://www.suse.com/Support/Doku/FAQ/</A</A>> and the archiv at <A HREF="http://www.suse.com/Mailinglists/suse-linux-e/index.html"><A HREF="http://www.suse.com/Mailinglists/suse-linux-e/index.html</A">http://www.suse.com/Mailinglists/suse-linux-e/index.html</A</A>>
Here's a perl script that you can use. Just call mfind at any directory level with a string you want to search for. It will recurse through every directory and scan the files. I use it many times every day. regs, -jrp Gordon E Heiitzman wrote:
I have to search all the files in a directory and its subdirectories and their subdirectories for a string.
I can search a directory at a time with fgrep 'string' *.*, but this is >taking ages to do for every dirictory.
Is their a way to grep recursively through the directories? I could not find anything like that in the manual.
Thanks >Nico -
A friend of mine has a utility called "tgrep" which does exactly what you describe. Has anyone else heard of this? If it's not available out there I could see about getting it to you. Gordy gordy@usjet.net
___________________________________________________________________ You don't need to buy Internet access to use free Internet e-mail. Get completely free e-mail from Juno at <A HREF="http://www.juno.com/getjuno.html"><A HREF="http://www.juno.com/getjuno.html</A">http://www.juno.com/getjuno.html</A</A>> or call Juno at (800) 654-JUNO [654-5866] - To get out of this list, please send email to majordomo@suse.com with this text in its body: unsubscribe suse-linux-e Check out the SuSE-FAQ at <A HREF="http://www.suse.com/Support/Doku/FAQ/"><A HREF="http://www.suse.com/Support/Doku/FAQ/</A">http://www.suse.com/Support/Doku/FAQ/</A</A>> and the archiv at <A HREF="http://www.suse.com/Mailinglists/suse-linux-e/index.html"><A HREF="http://www.suse.com/Mailinglists/suse-linux-e/index.html</A">http://www.suse.com/Mailinglists/suse-linux-e/index.html</A</A>> </PRE> <PRE> #!/usr/bin/perl # # This script looks for a string in every text file it # can find # # John Perser (1-12-97) rev1 # Brenden Tennant (7-15-97) added linenumber printouts # added -v flag to print out every occurance in each file # added -a flag to print out only the lines that contain all words in list # added -h flag to print out a help summary # added -q flag for quiet listing of filenames only #
# # printhelp prints a help screen to stdout. # sub printhelp { print "\n"; print " Usage for mfind:\n\n"; print " mfind [-hva] search_word [search_word] [search_word] [...]\n"; print " -h prints out this help text\n"; print " -v displays every line with a match in each file (not just the first)\n"; print " -a displays only the lines that contain all words to search on\n"; print " -w strips whitespace from the begining of matched lines\n"; print " -q prints a quiet listing with only filenames\n"; die "\n"; } # check command line parms for switches if (index($ARGV[0], "-") == 0) { $flags = shift; } else { $flags = ""; } # make sure there is something to search for if ($ARGV[0] eq "") { &printhelp; } # handle switches if (index($flags, "h") >= 0) { &printhelp; } if (index($flags, "v") >= 0) { $verbose = 1; print "Verbose listing ON.\n"; } else { $verbose = 0; } if (index($flags, "a") >= 0) { $andwords = 1; print "And word list ON.\n"; } else { $andwords = 0; } if (index($flags, "w") >= 0) { $stripwhitespace = 1; print "Whitespace striping ON.\n"; } else { $stripwhitespace = 0; } if (index($flags, "q") >= 0) { $nameonly = 1 } else { $nameonly = 0; } open ( FIND, "find . -print |") || die "Couldn't run find: $!\n"; FILE: while ($filename = <FIND>) { chop $filename; next FILE unless -T $filename; if ( !open (TEXTFILE, $filename )) { print STDERR "Can't open $filename -- continuing...\n"; next FILE; } $linenumber = 0; while ( <TEXTFILE> ) { $linenumber ++; if ($andwords == 0) { foreach $word (@ARGV) { if (index($_, $word) >= 0) { if ($nameonly == 1) { print "$filename\n"; next FILE; } if ($stripwhitespace == 1) { s/^\s*//; } print "$filename ($linenumber)-> $_"; if ($verbose == 0) { next FILE; } } } } else { $valid = 1; foreach $word (@ARGV) { if ((index($_, $word) >= 0) && ($valid == 1)) { $valid = 1; } else { $valid = 0; } } if ($valid == 1) { if ($stripwhitespace == 1) { s/^\s*//; } print "$filename ($linenumber)-> $_"; if ($verbose == 0) { next FILE; } } } } }
participants (2)
-
gordster@juno.com
-
jperser@airmail.net