Solved

Remove Unix files prior to specific date

Posted on 1998-11-16
9
443 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 84

Expert Comment

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

Expert Comment

by:tgreaser
Comment Utility
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
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
cd /usr/prod/rpt ; find . \! -name '.' -prune \( -name 'acct-aging.*' -mtime +30 -o -name 'mfg-trans.*' -mtime +45 \) -exec rm {} \;
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Expert Comment

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

Author Comment

by:drschank
Comment Utility
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
Comment Utility
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
Comment Utility
ozo nearly always only submits comments in preference to answers.
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
Comment Utility
#!/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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now