Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking to delete files older thatn "X" 2

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
I am trying to write a script that opens a directory on an HPUX 11.11 system, reads the directory and skips files that do not meet my regex, and then for all files that pass the regex, delete any file older than 90 days. I have been playing with the "-C" file test but it returns a number like "1072724171" which is the same for all the files it finds, even though I know only one file is created every day. Here is my code so far:

#!/opt/perl/bin/perl -w

use strict;

use vars qw($dirname @allfiles $file $day $month $year $created);

$dirname = "/tmp"; # This is a directory where data can be found
opendir(DIR,"$dirname");
@allfiles = readdir DIR;
foreach $file (@allfiles) {
next unless ($file =~ /^h\d+\.\d+/);
#$^T=time;
$created = (-C $file);
print "$^T\n";
}

Any help is appriciated.

Nick
 
If you don't have to do this in Perl, you could simply run the find command daily via a cron job. I do this on some Linux servers for certain directories (not sure if it works in HP UX or not). The following will delete all files older than 14 days in /tmp at 4:30 AM every day.

30 4 * * * find /tmp -type f -mtime +14 -exec rm {} \;

Chris
 
-M is the modified time. I believe this works.

Also, you could use perl's stat() function

use File::stat;
$stat = stat($file);
$created = $stat->ctime;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top