Hello Everyone,
I am trying to write a perl script that finds files that have not been accessed for x amount of days. I coded it using a system call with an unix find command:

system("find ${directory_path} -follow -type f -atime +${max_days} -exec /usr/bin/ls -lurt > $results_file {} \\;");

but I don't want to use a system call......I was thinking that I can use the File::Find module in perl, but the problem is that when I use atime (coming from stat(filename) it has the last access time in seconds since the epoch....any ideas?

Thanks.

Recommended Answers

All 4 Replies

Hello Everyone,
I am trying to write a perl script that finds files that have not been accessed for x amount of days. I coded it using a system call with an unix find command:

system("find ${directory_path} -follow -type f -atime +${max_days} -exec /usr/bin/ls -lurt > $results_file {} \\;");

but I don't want to use a system call......I was thinking that I can use the File::Find module in perl, but the problem is that when I use atime (coming from stat(filename) it has the last access time in seconds since the epoch....any ideas?

Thanks.

You are heading in the right direction using atime. But you don't need a system call. Just call it direct from Perl:

my ($readtime) = (stat ($filename))[8];

Then, as you say, you have the time in epoch seconds. So, now convert the current time to epoch seconds, and compare them.

Thanks Trudge for your response......I am still not sure how exactly to solve my problem though............for example if i want to find files that have not been accessed for more than 10 days, then the script will have this input:

listlogs.pl 10 /home/any/logs

like I said I have it working with the system call, because i can easily take that 10 days input and put it in the unix find command.......because i just put the atime option to +10 in the find command.

find /home/any/logs -type f -atime +10

In the case of the File::Find module inside perl......the atime that I get from the stat which you mentioned, give me the last time it was accessed since the epoch......i don't know how to use that to say....find the file if it has not been accessed for more than 10 days.

I thought it would be something like:

## $max_days is what i get from the input to the script.


my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($filename) or die "Unable to stat $_\n";


if ($atime > $max_days){
print "File has not been accessed for $max_days days: $File::Find::name\n";
}

Thats when i realized that atime is in seconds since epoch..........any further ideas with this or totally following another way?

Thanks!!

The -A file test operator is good for what you are trying to do. It will list the files last accessed date in days since the program started (your perl program).

So something like:

my @files = grep {(-f) && (-A) > 10} </home/any/logs/*>;
print "$_\n" for @files;

The parenthesis are important in the above code so make sure to include them. -f tests if the file is a regular file before testing the last accessed date, and the glob </home/any/logs/*> slurps all files and folders in from the directory.

Edit: if you need to drill down through subfolders then File::Find can be used and the -f and -A file test operators used as necessary.

Hello KevinADC,
This is exactly what I needed!!!! thanks!!!!!

Ed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.