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
Solved

Remove Unix files prior to specific date

Posted on 1998-11-16
9
451 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
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
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 several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

860 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