On Sunday 24 March 2002 13:45, zentara wrote:
On Sun, 24 Mar 2002 10:30:56 +0100
Clayton Cornell <c.cornell@chello.nl> wrote:
So, I want to search across multiple directories the way 'locate' does, but I don't want to use the database thing that 'locate' uses... I want to pattern match. Can 'find' do this? Is there a better way to do this from the console?
#find files recursively and find foo in them find . -type f -exec grep foo {} \;
OK, this (and the perl script) allows me to look into each file and find each instance of foo in each file. Assume I want to search for filenames... for example all jpegs in a directory tree. Something along the lines of find *.jpg in /home/user/mydirectory and all subdirs of mydirectory and have it only return the filenames... something like what dir *.jpg /s would do in DOS. Thanks for the perl script BTW... I am starting to teach myself perl, and this will go into my collection. C.
i#s one way to do it with find.
#Some people prefer to use xargs (look at man xargs. find . -type f | grep -v searchstring | xargs egrep;
I prefer perl because it allows you more control over customization of the script. Heres one I use: zgrep ############################################################ #!/usr/bin/perl # Recursively searchs down thru directories for pattern in files. # returns directory and line numbers too, and only searches text files. # usage zgrep 'search pattern' 'ext'(optional with no .) use warnings; use strict; use File::Find; my ($search, $ext) = @ARGV; if (defined $ext) {$ext = ".$ext"} else {$ext = '.*'}; die "Usage : greprz 'search' 'extension' (extension optional)\n" if ($search eq ""); @ARGV = (); find (sub { push @ARGV, $File::Find::name if (-f and -T and $ext or /\Q$ext$/)}, "."); while (<ARGV>) { close ARGV if eof; print "$ARGV: $. :$_\n" if /$search/; } #############################################################