LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script for removing files depending on age? (https://www.linuxquestions.org/questions/linux-newbie-8/script-for-removing-files-depending-on-age-884893/)

mensaman 06-06-2011 08:34 PM

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.

Tinkster 06-06-2011 08:41 PM

Hi, welcome to LQ!


And what is the problem you hit in that approach?



Cheers,
Tink

lonesoac0 06-07-2011 02:28 AM

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.

brownie_cookie 06-07-2011 02:42 AM

Quote:

Originally Posted by lonesoac0 (Post 4378527)
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.

grep Apr isn't dynamical, i mean, if he uses that, then he needs to adjust every month his script...
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`
        dirsize=`echo $statresult`

        if [ $dirsize -gt 0 ]; then
                VARm=`$find $MAIN/some/folder -name "*" -type f -mtime 90 | $wc -l`
               
                if [ $VARm -gt 0 ]; then
                        <code for deleting files>
                        echo '$Varm files which are older than  3 months'
                fi
      else
                        echo 'No files older than 3  months'
               
        fi

so what i do is, i search for files in a certain folder, when he founds some files, then he needs to delete them (but i don't know how to delete a group of files)
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 ;)

ssrameez 06-07-2011 03:52 AM

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";

}

mensaman 06-11-2011 09:55 PM

Quote:

Originally Posted by Tinkster (Post 4378329)
Hi, welcome to LQ!


And what is the problem you hit in that approach?



Cheers,
Tink


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.

mensaman 06-11-2011 09:58 PM

Quote:

Originally Posted by ssrameez (Post 4378586)
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######################


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! :)

mensaman 06-11-2011 10:02 PM

Quote:

Originally Posted by lonesoac0 (Post 4378527)
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.

ssrameez hit the nail on the head for what I was in need of, but this was very useful information, as well. Thank you.

Tinkster 06-11-2011 11:08 PM

Quote:

Originally Posted by mensaman (Post 4383041)
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! :)

Is it? From your description it sounded like you
wanted to delete them based on the name rather
than on a time-stamp. How odd.



Cheers,
Tink

mensaman 06-12-2011 02:07 PM

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.