Script for removing files depending on age?
I want to develop a bash or Perl script that will evaluate files in the local directory and delete every file that is older than a specified time period, such as three months.
I write documents and save the latest modifications by putting the current date in the filename, so I can easily see which versions are the newest. I would like to clean up my directory every once in a while, though, by eliminating the copies that are more than a few months old. Thanks for your help. |
Hi, welcome to LQ!
And what is the problem you hit in that approach? Cheers, Tink |
I presume that your problem is the script it self. I could not get all the way to full script status, but here is what I got so far..
ls -l | grep Apr | awk '{ print $9}' The first part is the ls command. You can sort your information however you want it sorted with this section... The second part sets WHAT you are looking for. You must be exact in what you type in or it will not work. I THINK that you maybe able to specify multiple months. The third part sets WHERE you are looking. In this case, the nineth field of ls. I would like to know if you get any further with your script. |
Quote:
I've got something similar. I search for files in certain folders, and when it's older than 2 days, i need to get a message so, with minor adjustements, this should work? Correct me if i'm wrong ;) Code:
statresult=`$find $MAIN/some/folder -type f | $wc -l` probably it needs a little bit of tweaking, but i think you're on your way now? i use a lot of variables, because i defined them at the start of my script, so in case that a location of a command has changed (for some reasons) i need to change only 1 piece of my code ;) |
Pasting the program which I am using.. Hope it may be useful.
This will remove files as well as directories. You can modify it accordingly. #!/usr/bin/perl use strict; use warnings; use File::Path; ##Variables used## ###########Sub routine###################### sub del_old { my $dir=$_[0]; my $age=$_[1]; print "Trying to open $dir\n"; opendir(D, "$dir") or die $!; chdir $dir; my $time = time(); while (my $f = readdir(D)) { next if $f =~ /^\./; my ($atime, $mtime, $ctime) = (stat($f))[8..10]; my $age_hours = ($time - $mtime) / 3600; my $age_days = int($age_hours / 24); next unless $age_days > $age; print "---> Deleting $f ($age_days days)..."; if (!-l $f && -d $f ) { rmtree($f) or warn "couldn't rmdir $f: $!"; } else { unlink $f; } print " done\n"; } closedir(D); } ##Main Starts Here## print "How many days old files or directories you want to keep:"; chomp(my $age=<STDIN>); chomp(my $dir=`pwd`); print "WARNING!!!!! Deleting Files and directories older than $age days from $dir\n"; print "Please enter 'I AGREE':"; chomp(my $agree=<STDIN>); if ($agree=~/I AGREE/){ print "Waiting for 5 seconds\n"; sleep 5; del_old($dir,$age); print "\n"; print "=" x 80,"Done\n"; print "\n"; } else { print "Coming out of program. Not deleting any directories or files\n"; } |
Quote:
Time. :) I have written a couple Perl scripts, but I still have much to learn and very little time to research it. A gentleman at work was looking for a way to delete all files past a particular age, but I was struggling to find the time to work it out myself. |
Quote:
This is perfect! Exactly what I was in need of, but lacked the time to figure it out. Thank you very much for your post! :) |
Quote:
|
Quote:
wanted to delete them based on the name rather than on a time-stamp. How odd. Cheers, Tink |
Tinkster,
I'm in the habit of including the revision date in the filename to make it easier on myself and others to determine which file represents the newest version, but without automatically replacing prior revisions, in case we want to look back at an earlier copy. The time stamp of the file, itself, will not only allow me to remove my personal files beyond a particular date, but will also allow me to do so for other files on the system, as well. So, this script is very helpful in accomplishing that broader application, in spite of the narrow scope of my initial post. :) |
All times are GMT -5. The time now is 02:16 PM. |