Expiration of files

2007-12-25 7:40:00

I wrote:

>

> Dear SUN Managers,

>

> in order to free disk space we are using the following 'find'

> command:

>

> find . -atime +365 -exec rm -f '{}' ';' -ls | awk '{sum+=$7; printf("%s %s\n",$7,$11)} END {printf("Anzahl: %d \Total File size: %d Bytes\n Durchschnitt %d\n",NR,sum,sum/NR)}' >> /usr/local/bin/expiration_data

>

>

> It works very well but since the -atime qualifier modified the access

> time of the directories, we can't remove empty directories which are

> fullfilling the expiration criteria +365.

>

> Any idea, clue to handle this situation?

> Of cource any hint is welcome and we will summarize.

>

>

Many thanks to all who had replied (see below):

****************

From: Steve Harris - Eaton Corp. - Beverly, MA - uunet!etnibsd!vsh

How about (not tested):

        #!/bin/sh

        find . -type d -atime +365 -print > /tmp/olddirs

        find . ! -type d -atime +365 -print > /tmp/oldfiles

        cat /tmp/oldfiles /tmp/olddirs |

        while read name

        do

                ls -lgdis $name

                rm -f $name

        done |

        awk '...' >> /usr/local/bin/expiration_data

********************

From: Kevin McElearney

-----------------------------------------------------------------------------

MIT Lincoln Laboratory Phone: (617) 981-3556

244 Wood Street

Lexington, MA 02173-9108 EMail: kevinmac@ll.mit.edu

What if you check EMPTY directories separately and looked at modification time?

*********************

From: sjc@hyperion.haystack.edu (Steve Cariglia)

The following should work, there must be something more elegant?:

      find . -atime +365 -exec rm.csh '{}' ';' -ls | awk '{sum+=$7; printf("%s %s\n",$7,$11)} END {printf("Anzahl: %d \Total File size: %d Bytes\n Durchschnitt %d\n",NR,sum,sum/NR)}' >> /usr/local/bin/expiration_data

where rm.csh contains:

#!/bin/csh -f

if ( -d $argv) then

   set qtst=`rmdir $argv |& cat`

else

   set qtst=`rm -f $argv |& cat`

   rmdir $argv:h >& /dev/null

endif

if ("$qtst" != "") then

   exit(1)

else

   exit(0)

endif

Don't forget to chmod 755 rm.csh

***********************

From: Jay Plett <jay@silence.princeton.nj.us>

You will be updating the access time of all the directories every

day when you run the find, so you will never have directories that

satisfy -atime +365, even if by some trickery you could avoid accessing

them before emptying them on this particular run of find.

So, how to solve the problem? Get gnu find. I added some stuff to

it that you might be able to make use of. If you could live with

removing all empty directories, regardless of age, then it would

be trivial:

        gfind . -depth \( -type f -atime +365 -exec rm -f {} \; \) -o \

                \( -type d -empty -exec rmdir {} \; \)

It would also be simple if you could remove all directories that

are at a depth of, say, 3 or more below the top directory:

        gfind . -type d -mindepth 3 -empty -exec rmdir ...

The only idea that comes to mind for accomplishing what I think

you really want is to keep track of which directories you removed

files from, then remove those directories if they are now empty.

        gfind . -type f -atime +365 -exec rm -f {} \; \

                -mindepth 1 -fprintf /tmp/dirlist '%h\n'

        gfind `sort -u /tmp/dirlist` -maxdepth 0 -empty -exec rmdir {} \;

        rm /tmp/dirlist

The second gfind runs the risk of overflowing the maximum length

of an argument list. You could write a shell script which reads

"sort -u /tmp/dirlist" a line at a time and runs a gfind for each

line.

        ...jay

***********************

From: Paul Begley <peb@sandoz7.ueci.com>

I had similar problems with using find for a backup script. I wanted

to find files which were x or less days old and collect the names and

use tar to back them up to tape.

The solution was using perl, a utility in the public domain from Larry

Wall. Perl has a find and a file flag capability which will do the

trick with a 2-3 line script. It will replace the one line AWK script

you are running as well. There is also a SUID facility for Perl which

will let you setup the script so it can be run by a user other than

root (which would not be an issue in your case, but was nice for some

things I wanted to do).

Hope this helps!

BTW - Perl is available via anon ftp from various sites and there is a

Programming Perl book available from O'Reilly Associates, publisher of

Nutshell Handbooks and other fine UNIX books and documentation.

***********************

From: keith@lgc.com

does rm -rf work?

************************

From: al!mdl@cypress.com (J. Matt Landrum)

I would be scared to do that. Can you use mtime instead of

atime?

========================================================================

========================================================================

So we are using the nice 'Gnu find' gfind command and it works very

well.

Again many thanks, especially to you Jay!!!

Rainer Blaes

Comments

Got something to say?

You must be logged in to post a comment.