Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Deleting files older then 48 hours..

by Kage (Scribe)
on Nov 12, 2001 at 18:27 UTC ( [id://124830]=perlquestion: print w/replies, xml ) Need Help??

Kage has asked for the wisdom of the Perl Monks concerning the following question:

Okay.. this is the script I whipped up:
#!/usr/bin/perl use File::stat; use Time::localtime; print "Content-type: text/html\n\n"; chdir( "/home/sscripts/public_html/kage/" ); local( @files ) = <*.zip>; foreach $file( @files ) { if( !-d $file ) { $date = ctime( stat( $file ) -> mtime); print "$file $date<br>\n"; } } #unlink @files; #Use that (somehwat) to delete the ones #that have been existant for more than #48 hours...
I want it to first, forget about a file called blahblah.zip, so it doesn't even see that file, then I want it to delet all files older then 48 hours... this script is going to be put on a Cron so no need to worry about the autorun part. the way it displays files is as follows:
geh.zip Mon Nov 12 04:03:24 2001 htrw5u.zip Mon Nov 12 06:17:29 2001 bht5iw354uy.zip Mon Nov 12 04:35:26 2001 hy543yyr.zip Sun Nov 11 21:05:06 2001 htrswu6.zip Mon Nov 12 06:21:15 2001 htre6iu.zip Mon Nov 12 06:16:14 2001 h653hhtr.zip Mon Nov 12 04:55:10 2001 ht5ewu675.zip Mon Nov 12 06:17:13 2001 blahblah.zip Mon Nov 12 04:21:26 2001 remember.. I want it to completely disreguard blahblah.zip and NOT del +ete it, no matter what it's age..
Any ideas?

Replies are listed 'Best First'.
Re: Deleting files older then 48 hours..
by stefan k (Curate) on Nov 12, 2001 at 18:40 UTC
    Hi,
    the first thing -of course- that comes to mind is that I would do this using the find command. One would use the mtime flag to set the (modification)time and would combine it with the name-checker (-name) to exclude blahblah.zip. Then pipe it into xargs rm -f and that's it.

    But then you seem to want to do this in perl. Simply put a check into your loop:

    foreach $file (<*.zip>) { next if $file =~ /blahblah/; # continue with filetype- and time-checking and # deletion... unless (-d $file) { # no dirs # use stat($file)[9] which is mtime in seconds, I # think, or use the -M filetest which gives days # to find out whether to delete or not... } }
    (Please note the regexp test (/blahblah/) could be replaced by string equality ( eq "blahblah.zip") when you really(!) need performance, and please lookup the return values of -M and stat since I'm not really sure of it).

    Maybe you should consider using File::Find for this...

    Regards... Stefan
    you begin bashing the string with a +42 regexp of confusion

Re: Deleting files older then 48 hours..
by rchiav (Deacon) on Nov 12, 2001 at 19:21 UTC
    File::Find is definately your friend for this. I have a script that I run on a win2k server to purge all files older that 2 weeks.

    Don't use this verbatum, but it should give you an idea of how to do what you're looking for. You're going to want to read up on File::Find and the _X file test operators to see what exactly I'm doing.

    There's also some ugliness with the log file name so ignore that. It's something that I need to fix but it hasn't been important enough to worry about.

    ################################################################### # purge_bp.pl # Purges files older than 14 days # Created by Rich Chiavaroli # Last modified on 2/14/01 # Change Log: # 2/5/01 - 2/7/01 Inital Creation # # 2/14/01 Fixed directory removal. All empty directories # will now be removed ################################################################### use strict; use File::Find; my ($logfile, $path,@dirlist, $i, $entry, @ignore_list, $ignore_file, @contents); # uncomment this line and set it to the drive the bypass is mapped to. #$path = "h:/*"; $logfile = "bypass.log"; # ignore.txt should have root directory names on seperate lines. $ignore_file = "c:/temp/ignore.txt"; open(IGNORE_FILE, $ignore_file) ||die "Can't open ignore file: $!\n"; @ignore_list = <IGNORE_FILE>; close IGNORE_FILE; chomp(@ignore_list); open( LOGFILE, ">>c:/temp/$logfile"); printf LOGFILE ("%d/%d/%d - Log of Purged documents for %s\n\n", (loca +ltime)[3,4], (localtime)[5] - 100, $path); while (<${path}> ) { if ( -d ) { push(@dirlist, $_ . "/") if ok_to_purge($_, @ignore_list); } } print LOGFILE "\n================================\n\n"; find ({wanted => \&process_file, no_chdir => 1, bydepth => 1 } , @dirl +ist); foreach $i (@dirlist) { unless (@contents = <$i/*>) { print LOGFILE "Directory $i empty - deleted.\n"; rmdir ($i); } } close LOGFILE; # Begin functions ################################################# ################################################# # This sub is passed each file to be processes. # Deletes all files older than 14 days along with empty # directories. ################################################# sub process_file { my($name, @contents); $name = $File::Find::name; #change /'s to \'s for the attrib command on windows platforms $name =~ s#/#\\#g; print "$name\n"; if (!-l && -d _) { unless (@contents = <$File::Find::name/*>) { if (rmdir($File::Find::name)) { print LOGFILE "Directory $name empty - deleted.\n" +; } elsif ((!`attrib -R $name`) && (rmdir($File::Find::name))) + { print LOGFILE "Directory $name empty - deleted(Read On +ly).\n"; } else { print LOGFILE "ERR: Can't delete $name - $!\n"; } } } else { my ($mdate, $cdate, $logfile, $age, $size, $max_age); $max_age = 14; $age = int(-M) < int(-C) ? int(-M) : int(-C); if ($age > $max_age) { $size = (-s); if (unlink($File::Find::name) ) { print "$File::Find::name, - $age days, $size \n"; print LOGFILE "$File::Find::name, - $age days, $size \ +n"; } elsif ((!`attrib -R $name`) && (unlink($File::Find::name)) +) { print LOGFILE "$File::Find::name, - $age days, $size ( +Read Only)\n\n"; } else { print LOGFILE "ERR: Couldn't delete $File::Find::name +- $!\n"; } } } } ################################################### # Checks the directory to see if it's in the ignore file. ################################################### sub ok_to_purge { my ($purge, $i, $dir, @list); $purge = 1; ($dir, @list) = @_; foreach $i (@list) { if ($dir =~ /^$path\/$i/i) { print LOGFILE "Ignoring directory: $dir\n"; $purge = 0; last } } return $purge; }
Re: Deleting files older then 48 hours..
by sifukurt (Hermit) on Nov 12, 2001 at 19:58 UTC
    In the spirit of TMTOWTDI, here's a quick script that doesn't require any modules other than strict:
    use strict; my $dir = "/home/sscripts/public_html/kage/"; my %ignore = ( 'blahblah.zip' => 1 ); my $seconds = 60 * 60 * 48; my $now = time(); print ("Content-type: text/html\n\n"); opendir ITEMS, $dir; my @files = grep !/^\./, readdir ITEMS; closedir ITEMS; chomp @files; chdir $dir; foreach my $file ( @files ) { if ( -d $file || defined $ignore{$file} ) { next } my $mod_time = ( stat($file) )[9]; my $when = localtime( $mod_time ); print ("$file $when<br>\n"); if ( $now - $seconds > $mod_time ) { unlink $file; } }
    That's my 16**(1/4) cents. HTH
    ___________________
    Kurt
Re: Deleting files older then 48 hours..
by Jazz (Curate) on Nov 13, 2001 at 01:20 UTC
    my $days = 2; my $keep_as_of = time - ( $days * 84600 ); my @old_files = ( grep{ !-d && ! /\.zip$/i && ( stat( $_ ) )[9] < $keep_as_of } glob( "/path/to/directory/*" ) ); unlink @old_files; print "The following files have been removed\n\n", join "\n", @old_fil +es;
Re: Deleting files older then 48 hours..
by strredwolf (Chaplain) on Nov 13, 2001 at 10:30 UTC
    This isn't even a job for a Perl script.

    find . -type f -a -mtime +2 to find the files. grep -v "blahblah" to strip out the ones you want to ignore, and xargs rm to do the final nuking. All in one line now...

    find . -type f -a -mtime +2 | grep -v "blahblah" | xargs rm

    --
    $Stalag99{"URL"}="http://stalag99.keenspace.com";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://124830]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-20 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found