Solved

Remove Unix files prior to specific date

Posted on 1998-11-16
9
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 84

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

 
LVL 84

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 84

Accepted Solution

by:
ozo earned 100 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

Technology Partners: 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

Suggested Solutions

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

756 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