By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

delete files if there are more than 8 files in a directory

P: 2
Hi!
I need a script which delete files in a directory when there are more than 8 files exist in that directory. It should delete oldest file and 3 latest file should be remains in that directory.

file format : read_story_12_03_2010.zip
read_story_13_03_2010.zip

Thanks in advance for ur help.
Mar 30 '10 #1
Share this Question
Share on Google+
1 Reply


P: 2
Hi!
i have written below program:
Expand|Select|Wrap|Line Numbers
  1. my $dir'/usr/local/new;
  2. my %files;
  3.  
  4.  opendir(DIR, "$dir") or die "Cannot open dir \"$dir\": " . $!;
  5.        my $time = time();
  6.  
  7.        foreach my $f1 (readdir(DIR)) { 
  8.           $files{$f1} =  ((stat("$dir/$f1"))[9]);}
  9.         foreach my $f (reverse sort  { $files{$b} <=> $files{$a} } keys(%files)) {
  10.            next if $f =~ /^\./;
  11.            next if $f !~ /^read_story*.rpm/;
  12.        my ($atime, $mtime, $ctime) = (stat("$dir/$f"))[8..10];
  13.        my $age_hours = ($time - $mtime) / 3600;
  14.        my $age_min = ($time - $mtime) / 60;
  15.        my $age_days  = int($age_hours / 24);
  16.            print "$dir/f\n";
  17. }
  18.  
but its only giving list of files in a directory.
Now i want to remove files if there are more than 8 file exist in that directory.I want only 3 files at a time.
How can i do it?

Thanx in advance
Mar 31 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.