Remove Unix files prior to specific date

Posted on 1998-11-16
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?
Question by:drschank
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
LVL 84

Expert Comment

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

Expert Comment

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


Author Comment

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?
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

LVL 84

Expert Comment

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

Expert Comment

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

Author Comment

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?

Expert Comment

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

Expert Comment

ID: 2007898
ozo nearly always only submits comments in preference to answers.
LVL 84

Accepted Solution

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;

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 have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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.:

724 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