find files from the past 7 days

Hi All,

I have a file which contains the listing of another directory:

>cat list.dat
-rwxr-xr-x 1 test staff 10240 Oct 02 06:53 test.txtdd
-rwxrwxrwx 1 test staff 0 Oct 04 07:22 test.txx
-rwxrwxrwx 1 test staff 132 Sep 16 2007 test_tt.sh
-rwxrwxrwx 1 test staff 193 Aug19 2007 test_ttt.sh
-rwxrwxrwx 1 test staff 45 Nov 21 2007 testfile.sh
-rw-r--r-- 1 test staff 10240 Apr 18 06:49 testtrar.tar
>

I need to extract the names of the files which are created in the last 7 days. So the output of the script will be lilke below from the above file:

-rwxr-xr-x 1 test staff 10240 Oct 02 06:53 test.txtdd
-rwxrwxrwx 1 test staff 0 Oct 04 07:22 test.txx

Is there any way to achieve this?

Thanks,
D

The "easy" way is to re-generate the list using

find $DIR -mtime -7 -type f

The "hard" way involves creating a set of regular expressions based on the current date/time and range. The "logwatch" utility has a perlmodule for doing exactly this sort of thing. Respond and I'll post more info.

The really hard way is to calculate the timestamp of the file in seconds based on the ls output and compare that to the current time minus 7 days of seconds (7*24*3600). It might not be as hard as I think, but it involves some straight coding (in Perl or awk) and probably lots of debugging.

I did this recently in perl this way.

$days = 7;
$dir = "/opt/application";
opendir(BIN, $dir) or die "Can't open $dir: $!";
while(defined($file = readdir BIN) ){
    next if $file =~ /^\.\.?$/;  #skip . and ..
    if(int(-M $file) < $days){
        # do something
    }
}

This only difference is you would read from file.

thanks for that.

I cant use the find command as i dont have the acces to the directory. The directory is accessed by ftp. I ftp to the remote machine and get the listing from the directory into this particular file. And then from this file I need to filter out files older than 7 days. So I think only way is to use the file.

In that case the timestamp method might be the way left I guess...

Hi Photon - I have little knowlegde about perl so I cant understand what your script does. I think its opening the directory and taking each file and checking for conditions. But can that be done with the contents of a file?

Thanks again for your help
D

or simply can i do the below check:
I have the timestamp of a file in a variable

ts="29 Sep 09:19"

How can I find out if this falls in last 7 days?

I don't see why not.

$days = 7;
open FILE, "/opt/application/file.txt";
while(my $line = <FILE>){
    if(int(-M $line) < $days){
        #do something
    }
}

There may be other wazs, but here's what I'd do. Install "logwatch". Let's say it installs into /usr/share/logwatch. Then make a little perl-script:

#!/usr/bin/perl
use lib "/usr/share/logwatch/lib";
use Logwatch ':dates';

$ENV{"LOGWATCH_DATE_RANGE"}="since -7 days";
my $SearchDate = TimeFilter('%b %d %H:%M');

while (<>) {
   print if m/\b$SearchDate\b/o;
}

Then do perl <scriptname> <filename>

You should have what you are looking for.