Files and Strings

Associate
Joined
1 Aug 2003
Posts
1,053
I need to find several files that were made within a certain date and contain certain text. Clearly I'm making a pig's ear out of it, but my first thoughts were something like this....

find ./ -iname "*.server" -mtime -19 | grep -H "string"

Advice would be more than welcome...
 
you want to search inside the files? then you were almost there :

Code:
find -iname "*.server" -mtime -19 -type f -print0 | xargs -0 grep "string"
(the -print0 / -0 handles spaces in filenames quite nicely)

The time/date matching options of GNU find are rather limited, you could try a Perl script if you want more flexibility :
Code:
#!/usr/bin/env perl

use File::Find;
use File::stat;
use Time::Local;

$t1 = timelocal(0,0,0,1,1,2011); # start date - sec,min,hour,day,month,year
$t2 = timelocal(0,0,0,1,2,2011); # end date
$s = "string";

sub each_file {
  return unless -f;
  $t = stat($_)->ctime;
  if ($t > $t1 && $t < $t2 && (`grep -c "$s" "$_"` > 0)) { 
    print "${File::Find::dir}/$_\n"; 
  }
}

find(\&each_file, ('.'));

Also, for a bit of fun, try typing : find2perl -iname "*.server" -mtime -19 -type f
:)
 
Last edited:
Thank you so much, that completely sorted the problem.

Slightly faster than writing the perl script I found was to perform two time sensitive searches and output the values to text files and then compare the two to find matches resulting in a list of files made between two dates :)

Possibly a little more complicated, but I found it easier than writing a perl script.
 
Back
Top Bottom