From: krab@iesd.auc.dk (Kresten Krab Thorup)
Newsgroups: comp.emacs
Subject: cleanup.pl (was: Re: auc-tex)
Date: 7 Apr 92 16:12:46 GMT
Article-I.D.: eos.KRAB.92Apr7171246
	<KRAB.92Mar30235718@purple.iesd.auc.dk>
Organization: Mathematics and Computer Science, Aalborg University

Hi netters.

I've had a lot of request for this, so I will now post this perl
script.

Format of the cleanuprc is:

	[Unix regexp] [days to save]

A (site) standard could be installed in 

	/usr/local/lib/cleanuprc

But a user may specify his own in 

	$HOME/.cleanuprc

Have fun!

/Kresten

Now follows 3 files: a sample cleanuprc, the cleanup program, and it's
manual page.

--cleanuprc---------------------------------------------------
.*~$    2
^#.*#$  2
.*\.log$ 1
.*\.aux$ 1
.*\.ilg$ 1
.*\.idx$ 7
.*\.ind$ 7
^core$  0
^.*\.o$ 7
^a.out$ 7
----------------------------------------------------------------------

--cleanup-------------------------------------------------------------
#!/bin/perl
## clean
## 
## Author          : Lars P. Fischer
## Created On      : Fri Nov 24 10:52:45 1989
## Last Modified By: Lars P. Fischer
## Last Modified On: Sun Nov 26 20:16:18 1989
## Update Count    : 29
## Status          : Alpha!
## HISTORY
## 26-Nov-1989		Lars P. Fischer	
##    Last Modified: Sun Nov 26 20:10:29 1989 #27 (Lars P. Fischer)
##    Added /home/local/lib/...
## 26-Nov-1989		Lars P. Fischer	
##   Last Modified: Sun Nov 26 02:47:02 1989 #25 (Lars P. Fischer)
##   Reads /usr/local/lib/cleanuprc
## 24-Nov-1989		Lars P. Fischer	
##    Last Modified: Fri Nov 24 11:11:06 1989 #7 (Lars P. Fischer)
##    Started using fancy headers. Changed to use find instead of
##    doing own file-tree-walk. Using fixed getopts.
## 
## Copyright (C) 1989 Lars P. Fischer. All Rights Reserved

do "getopts.pl";
do "stat.pl";

$time_now = time;
$secs_per_day = 60*60*24;

## Handle options
do Getopts ('pv');
exit 1 if $err_opt;
$print_only = 1 if ($opt_p);	## just print files slated for deletion
$verbose = 1 if ($opt_v);	## verbose -- tell what we're doing

## -b => background?

push (dir_stack, shift) while ($#ARGV >= 0);
($#dir_stack >= 0) || push (dir_stack, ".");
## check if all the specified dir's are there ???

do get_clean_list();		## read the specification file into array
				## clean_list
## go for it
while ($#dir_stack >= 0) {
  $dir = pop (dir_stack);
  open (in, "find $dir ! -type d -print|") || die "can't run find";
  while (<in>) {
    chop;
    do test_crit ($_);
  }
  close (in);
}

exit(0);

## called two args: the full file name of a file, and the tail of that
## name. Test if the tail matches any of the (reg,age) specs. If so,
## take appropriate action on full file name.
sub test_crit
{
    local ($fil, %rest) = @_;
    local ($age, $ftime, $reg, $tail);

    ($tail) = ($fil =~ m|.*/([^/]*)|);
    do Stat ($fil);
    $age = int(($time_now - $st_atime) / $secs_per_day);
    while (($reg, $ftime) = each %clean_list) {
        if ($age >= $ftime && $tail =~ m/$reg/) {
            print $fil, "\n" if ($print_only || $verbose);
	    unlink ($fil) if (! $print_only);
	    ## test if unlink fails. Maybe check if file is write-protected.
	    ## if so, should we delete (maybe option -f??)
            return;
        }
    }
}

## open .cleanuprc file and read contents into "clean_list", a mapping from
## regular expressions to ages.
sub get_clean_list
{
    local (@F, $cl_file_name);

    $cl_file_name = $ENV{"HOME"} . "/.cleanuprc";
    open (cl_file, $cl_file_name) 
	|| open (cl_file, "/home/local/lib/cleanuprc") 
	|| open (cl_file, "/usr/local/lib/cleanuprc") 
	|| die ("couldn't open $cl_file_name\n");
    
    while (<cl_file>) {
	chop;
	@F = split(/[ \t]+/);
	$clean_list{$F[0]} = $F[1];
	## should check that reg does not include a "/", that's not legal
    }
}
----------------------------------------------------------------------

----cleanup.man-------------------------------------------------------
.TH CLEANUP 1
.SH NAME
cleanup \- kill waste files
.SH SYNOPSIS
.B cleanup
[-v] [-p] [dirs...]
.SH DESCRIPTION
.I Cleanup
removes 
.I waste
files from the file system. I waste file is defined as a file created
as a side effect of some activity and holding information that is no
longer relevant. Examples of such files are DVI files created by TeX,
core files etc.
.PP
Waste files are specified by a regular expression, r, and a
number, n.
If the name of a file matches r and the file has
not been accessed for n days, the file is a waste file. The regular
expressions are given in the same syntax as used by 
.IR grep (1).
Example:
.br
.in +0.5i
 .*\\.dvi$	7
.br
.in -0.5i
This says that files with names ending in ".dvi" can be deleted if
they have not been accessed for 7 days.
.PP
.I Cleanup
takes a set of such specifications from a file called the 
.I kill
file, and deletes all files matching any
of the specifications. As default, the kill file is 
"/usr/local/lib/cleanuprc" but the user can override this by
creating her own kill file. This file should be named ".cleanuprc"
and placed in the users home directory. Use the
default file as a model when creating your own file or to find out
what the defaults are.
.SH EXAMPLE
.I Cleanup
would typically be used from a users ".logout" file. By putting the line
.br
.in +0.5i
cd; cleanup &
.br
.in -0.5i
in the ".logout" file, all waste files will be killed when the user
logs out. This enables the user to once and for all specify the files
to be deleted.
.SH OPTIONS
.IP -v
verbose mode. Print the names of the files as they are deleted.
.IP -p
print only. Do not delete any files, just print the names of files
that would have been deleted. Useful for debugging ".cleanuprc" files.
.SH FILES
.ta \w'/usr/local/lib/cleanuprc  'u
/usr/local/lib/cleanuprc	default kill file
.br
~/.cleanuprc	users own kill file
.SH "SEE ALSO"
.IR grep (1)
.SH DIAGNOSTICS
Returns 0 on success, 1 otherwise. Error-messages printed to stderr.
.SH BUGS
Please report if noted.
.SH AUTHOR
Lars Fischer, fischer@iesd.auc.dk
----------------------------------------------------------------------
Newsgroups: comp.emacs