?
Solved

Remove Unix files prior to specific date

Posted on 1998-11-16
9
Medium Priority
?
459 Views
Last Modified: 2010-04-21
I would like to be able to remove files from our DG/Unix system based on the date the files were created.  Ideally, I would like to maintain a flat file of filenames and their days to expire and then remove the files that have a date prior to the date of today minus the days to expire.

For example the flat file would have records such as the following:

/usr/prod/rpt/acct-aging.*      30
/usr/prod/rpt/mfg-trans.*       45

With this example, all acct-aging files would be deleted after 30 days and mfg-trans after 45 days.

Is there any utility that exists or does somebody have a script that would allow this?
0
Comment
Question by:drschank
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 2007891
find /usr/prod/rpt/ -name 'acct-aging.*' -mtime +30 -exec rm {} \;
0
 

Expert Comment

by:tgreaser
ID: 2007892
it looks like ozo showed you how to do this but if I was you I would but that in a cron job and instead of  doing
-exec rm I would move the files to a dir if you have the space
and let them sit there for a week or so..  You know how users are....... but if you already have a back up then just do what
ozo said

0
 

Author Comment

by:drschank
ID: 2007893
The only problem with ozo's solution is that it will recursively check subdirectories.  The flat file that we have created specifies a definitive path.  I have read the man pages for the find command and it shows a -prune option but as I understand it, each subdirectory would need to be specified to be omitted from the command.  Is this correct?  Is there any other option?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 85

Expert Comment

by:ozo
ID: 2007894
cd /usr/prod/rpt ; find . \! -name '.' -prune \( -name 'acct-aging.*' -mtime +30 -o -name 'mfg-trans.*' -mtime +45 \) -exec rm {} \;
0
 
LVL 2

Expert Comment

by:gothick
ID: 2007895
If you're testing for creation time, don't you want -ctime, not -mtime?
0
 

Author Comment

by:drschank
ID: 2007896
Thanks ozo.  You have saved me tons of frustration!  And thanks to tgreaser and gothick for their input.  This is the first time I have used Experts Exchange, how do I grade ozo and close out this question?
0
 
LVL 2

Expert Comment

by:gothick
ID: 2007897
I believe ozo's reply to be submitted as an answer (rather than a comment) before it is graded.
0
 
LVL 3

Expert Comment

by:braveheart
ID: 2007898
ozo nearly always only submits comments in preference to answers.
0
 
LVL 85

Accepted Solution

by:
ozo earned 400 total points
ID: 2007899
#!/usr/bin/perl -w
open(F,"<flat.file") or die "can't open file of filenames: $!";
while( <F> ){
        ($files,$age) = split;
        ($dir,$glob) = ($files =~ m:(.*)/(.*):) or die "no  directory specified: $!";
        opendir(D,$dir) or die "can't open $dir:$!";
        $pat = &fileglob_to_re($glob);
        for( grep /$pat/ && -M "$dir/$_" > $age, readdir D ){
                unlink("$dir/$_") or warn "$dir/$_: $!\n";
        }
        closedir D;
}
sub fileglob_to_re {
    local($tmp) = @_;
    $tmp =~ s#([./^\$()])#\\$1#g;
    $tmp =~ s/([?*])/.$1/g;
    "^$tmp\$";
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question