Thread View: de.comp.lang.perl
1 messages
1 total messages
Started by Andreas Holzmann
Thu, 30 Jul 1992 18:19
mrm
Author: Andreas Holzmann
Date: Thu, 30 Jul 1992 18:19
Date: Thu, 30 Jul 1992 18:19
502 lines
12612 bytes
12612 bytes
Hallo!
Ich habe mich in letzter Zeit etwas mit perl beschaeftigt und
herausgekommen ist dabei dieses Programm. Schickt mir doch eine Mail
und sagt mir was ihr davon haltet, was man einfacher machen koennte,
oder was man noch hinzufuegen solte.
Ach ja, vielleicht sollte ich noch sagen was das Programm macht. :)
Es loescht Dateien z.B. emacs-Backupfiles oder C-Objectdateien usw...
Ausserdem kann es etwas mehr, was rm nicht kann. Neugierig? Also
gleich mal ausprobieren...
tschuess
Andreas
----------- schnipp - schnapp - schnippschnipp ----------
#!/bin/sh
# This is a shell archive (produced by shar 3.49)
# To extract the files from this archive, save it to a file, remove
# everything above the "!/bin/sh" line above, and type "sh file_name".
#
# made 07/30/1992 17:52 UTC by holzmann@akira
# Source directory /home/holzmann/perl/mrm
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length mode name
# ------ ---------- ------------------------------------------
# 1065 -r--r--r-- README
# 3174 -r--r--r-- mrm.1
# 4395 -rwxr-xr-x mrm
# 143 -r--r--r-- Makefile
#
# ============= README ==============
if test -f 'README' -a X"$1" != X"-c"; then
echo 'x - skipping README (File already exists)'
else
echo 'x - extracting README (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'README' &&
@(#) README for Release 1.1 of mrm (Tue Apr 7 15:04:58 MET DST 1992)
X
X mrm is a small perl-script that I wrote to keep my $HOME
X clean. It's my first encounter with Perl... so please tell me
X if something could be done easier than I've tried it :)
X
Description:
X
X mrm removes files according to filename extensions, eg. all
X Emacs backup-files (*~) and Emacs autosave-files (#*#). If you
X want to keep such files a few days (You never can tell! :) you
X can remove files which haven't been accessed since 7 days.
X
X mrm is distributed in the hope that it will be useful, but
X WITHOUT ANY WARRANTY. No author or distributor accepts
X responsibility to anyone for the consequences of using it or
X for whether it serves any particular purpose or works at all,
X unless he says so in writing.
X
X
Programs included:
X
X mrm - special file remover
X
X mrm.1 - manpage for mrm
X
X README - this file
X
X Makefile- simply the makefile for easy installing :)
X
X
X
X
Suggestions and comments welcome...
X
Andreas Holzmann holzmann@kirk.fmi.uni-passau.de
SHAR_EOF
chmod 0444 README ||
echo 'restore of README failed'
Wc_c="`wc -c < 'README'`"
test 1065 -eq "$Wc_c" ||
echo 'README: original size 1065, current size' "$Wc_c"
fi
# ============= mrm.1 ==============
if test -f 'mrm.1' -a X"$1" != X"-c"; then
echo 'x - skipping mrm.1 (File already exists)'
else
echo 'x - extracting mrm.1 (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'mrm.1' &&
.\" @(#) - Manpage for mrm
.TH MRM 1 05/18/92
.SH NAME
mrm \- searches for special files and removes them
.SH SYNOPSIS
.B mrm
.B \-aetcbm
[
.B \-f | \-n
]
[
.B \-z "\fItime\fR
]
[
.I directory...
]
.SH DESCRIPTION
\fImrm\fR removes files according to filename extensions, eg. all
Emacs backup-files (*~) and Emacs autosave-files (#*#). If you want to
keep such files a few days (You never can tell! :) you can remove
files which haven't been accessed since \fItime\fR days.
.PP
If one or more
.I directory
is given
.I mrm
is looking in them for files. If
.I directory
is omitted
.I mrm
starts at $HOME.
.PP
If a directory contains the file
.I .mrmskip
no file in this directory is removed. But subdirectories in that
directory are accessed normal.
.PP
.SH OPTIONS
.TP 1i
.B \-e
Removes Emacs-files. (
.BR *~ ,
.BR #*#
)
.TP
.B \-t
Removes TeX-files. (
.BR *.dvi ,
.BR *.aux ,
.BR *.bbl ,
.BR *.blg ,
.BR *.lof ,
.BR *.lot ,
.BR *.log ,
.BR *.toc ,
.BR *.idx
).
.TP
.B \-c
Removes object-files. (
.BR *.o ,
.BR core ,
.BR a.out
).
.TP
.B \-b
Removes backup-files. (
.BR *.bak ,
.BR *%
)
.TP
.B \-m
Removes mail-backups. (
.BR .article ,
.BR dead.letter
)
.TP
.B \-a
Same as all the options above together (-etcbm).
.TP
.B \-f
Force files to be removed without asking questions.
.TP
.B \-n
No execution mode. Prints all files which will be removed, but don't
delete them.
.TP
.BI \-z time
Removes only if the files haven't been accessed since
.I time
days.
.SH CUSTOMIZATION
You've got the possibility to do some setup in the .mrmrc-file. Here
you can preset the options or the time (-z option). You also can
exclude some special files from removing (eg. you always want to
delete TeX-files but keep the .dvi's).
.PP
You can use the following commands in the \fI.mrmrc\fR-file:
.TP
.B a: \fIfile\fR
see rmalways...
.TP
.B defopt=\fIoptions\fR
Presets the options.
.TP
.B deftime=\fItime\fR
Presets the time (see option -z).
.TP
.B force
Removes all files without asking (see option -f).
.TP
.B n: \fIfile\fR
see rmnot...
.TP
.B rmalways: \fIfile\fR
Removes \fIfile\fR without asking.
.TP
.B rmnot: \fIfile\fR
Skips \fIfile\fR. (Do not remove it!)
.PP
\fIfile\fR can include \fIRegular Expressions\fR (see Perl(1) or ed(1)
for further information). If a file matches (according to regular
expressions) delnot AND delalways mrm skips this file.
.PP
example:
.RS
.sp .5
.nf
.B
# removes without asking (same as -f option)
force
X
# never remove *.dvi-files
n: .*\\.dvi
X
# removes only files which are older than 3 days
deftime = 3
X
# remove always TeX-Files and mail-backups when using mrm
defopt=-tm
.fi
.RE
.LP
X
A list of all options:
.nf
X a \h'|2i' see delalways
X force \h'|2i' removes without asking (see option -f)
X defopt \h'|2i' option preset by user
X deftime \h'|2i' time preset by user
X n \h'|2i' see delnot
X rmalways \h'|2i' file(s) which should be removed without asking
X rmnot \h'|2i' file(s) which should be never deleted
.fi
.SH FILES
.PD 0
.TP 20
.B .mrmskip
.TP
.B \s-1$HOME\s0/\fB.\fPmrmrc
.PD
.SH SEE ALSO
.BR ed (1),
.BR perl (1),
.BR rm (1)
.SH AUTHOR
Andreas Holzmann (holzmann\|@kirk.fmi.uni-passau.de)
.SH COPYRIGHT
Copyright 1992 by Andreas Holzmann
SHAR_EOF
chmod 0444 mrm.1 ||
echo 'restore of mrm.1 failed'
Wc_c="`wc -c < 'mrm.1'`"
test 3174 -eq "$Wc_c" ||
echo 'mrm.1: original size 3174, current size' "$Wc_c"
fi
# ============= mrm ==============
if test -f 'mrm' -a X"$1" != X"-c"; then
echo 'x - skipping mrm (File already exists)'
else
echo 'x - extracting mrm (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'mrm' &&
#!/usr/local/bin/perl
# @(#) mrm v1.1 ah
X
# mrm is searching for special files and removes them
# Syntax:
# mrm (-e|-t|-c|-a) [-f|-n] [-z<time>] [directory...]
# -e : removes emacs-backups (*~, #*#)
# -t : removes tex-files (*.dvi, *.aux, *.bbl, *.blg, *.lof, *.lot,
# *.log, *.toc, *.idx)
# -c : removes *.o-files (*.o, core, a.out)
# -b : removes backups (*.bak, *%)
# -m : removes mail-backups (.article, dead.letter)
# The upper four options can be combined.
# (eg. -ec : removes emacsbackups and .o-files)
# -a : same as all options above together (-etcbm)
# -f : removes without asking questions
# -n : print files which will be removed, but don't delete them
# -z<time> : removes files which haven't been accessed since <time>
# days
#
# Please read the manpage (mrm(1)) for further details.
#
# Andreas Holzmann holzmann@kirk.fmi.uni-passau.de
X
# read Getopts and check options
require 'getopts.pl';
do Getopts('nfz:aetcbm');
X
# check .mrmrc and preset
open(MRMRC,"$ENV{HOME}/.mrmrc");
while(<MRMRC>)
{
X # force-option
X if (/force/)
X {
X $opt_f=1;
X }
X
X # defopt-option
X if (/defopt\s*=\s*-([etcbm]+)/)
X {
X $test=$1;
X while ($test=~/^(.)(.*)/)
X {
X ($first,$rest) = ($1,$2);
X eval "\$opt_$first = 1";
X $test=$rest;
X }
X }
X
X # deftime-option
X if (/deftime\s*=\s*(\d)/)
X {
X eval "\$opt_z = $+";
X }
X
X # rmnot-option
X if (/(rmnot|n):\s*(.+)/)
X {
X push(@delnot,$+);
X }
X
X # rmalways-option
X if (/(rmalways|a):\s*(.+)/)
X {
X push(@delalways,$+);
X }
}
close(MRMRC);
X
# valid time-option?
if ($opt_z)
{
X $opt_z=~/\D/ && die "There must be a number after the -z option!";
}
X
# translate options and set $check
push(@check,'.*~$|^#.*#$') if $opt_e||$opt_a;
push(@check,'.*\.o$|^core$|^a\.out$') if $opt_c||$opt_a;
push(@check,'.*\.(dvi|aux|bbl|blg|lot|lof|log|toc|idx)$') if $opt_t||$opt_a;
push(@check,'.*(\.bak|%)$') if $opt_b||$opt_a;
push(@check,'^\.article$|^dead\.(letter|article)$|^Cancelled\.mail$') if $opt_m||$opt_a;
X
if ($check[0] eq '')
{
X printf "Usage: mrm (-e|t|c|b|m|a) [-f|-n] [-z<time>] [directory...]\n";
X exit;
}
X
# scan all dirs user has given me
if (defined $ARGV[0])
{
X for (@ARGV)
X {
X &dodir($_,0);
X chdir '..' unless $_ eq '.';
X }
}
else
{
X # no dir? -> let's take the current one
X &dodir('.',0);
}
exit(0);
X
# Checks if $filetocheck is in array @checklist
# (checks regular expressions)
sub inlist
{
X local($filetocheck,@checklist)=@_;
X for(@checklist)
X {
X return(1) if $filetocheck=~/^($_)$/;
X }
X return(0);
}
X
# delete it or not
sub delete
{
X local($filename)=local($path)=@_;
X $filename =~ s!^.*/!!;
X
X # filename in delnot-array
X if (&inlist($filename,@delnot))
X {
X print "... $path skipped !\n";
X return;
X }
X
X # filename in delalways-array
X local($opt_f)=1 if &inlist($filename,@delalways);
X if(&inlist($filename,@check))
X {
X # if $opt_z and filename is not old enough -> return
X $opt_z && $opt_z > -A $filename && return;
X # only print filename, don't remove it
X if ($opt_n)
X {
X print "$path will be removed!\n";
X return;
X }
X # remove without asking
X if ($opt_f)
X {
X unlink("$filename");
X print "$path removed!\n";
X }
X else
X {
X print "mrm: remove $path? ";
X <STDIN>=~/^[Yy]$/ && unlink("$filename");
X }
X }
}
X
# scaning the directory and its subdirectories
sub dodir
{
X local ($dir,$nlink)=@_;
X local ($skip,$dev,$ino,$mode,$subcount);
X
X unless ($nlink)
X {
X ($dev,$ino,$mode,$nlink)=stat($dir);
X chdir $dir || die "Can't CD to $dir!";
X }
X
X # read all filenames of the current dir
X opendir (DIR,'.') || die "Can't open $dir";
X local (@filenames)=readdir(DIR);
X closedir(DIR);
X
X # skip this dir if it contains the file '.mrmskip'
X for (@filenames)
X {
X if ($_ eq '.mrmskip')
X {
X $skip=1;
X print "...skipping $dir !\n";
X }
X }
X
X $subcount=$nlink-2;
X for (@filenames)
X {
X # don't delete this two files :-)
X next if $_ eq '.';
X next if $_ eq '..';
X $name="$dir/$_";
X
X # if it's no dir and we're not skipping
X &delete($name) unless -d $_ || $skip==1;
X next if $subcount==0;
X ($dev,$ino,$mode,$nlink)=lstat($_);
X next unless -d _;
X
X # if $name is a directory
X chdir $_ || die "Can't CD to $name!";
X &dodir($name,$nlink);
X chdir '..';
X --$subcount;
X }
}
SHAR_EOF
chmod 0755 mrm ||
echo 'restore of mrm failed'
Wc_c="`wc -c < 'mrm'`"
test 4395 -eq "$Wc_c" ||
echo 'mrm: original size 4395, current size' "$Wc_c"
fi
# ============= Makefile ==============
if test -f 'Makefile' -a X"$1" != X"-c"; then
echo 'x - skipping Makefile (File already exists)'
else
echo 'x - extracting Makefile (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'Makefile' &&
# @(#) Makefile - Makefile for mrm
X
BINDIR=/usr/local/bin
MANDIR=/usr/local/man/man1
X
install: mrm mrm.1
X cp mrm ${BINDIR}
X cp mrm.1 ${MANDIR}
SHAR_EOF
chmod 0444 Makefile ||
echo 'restore of Makefile failed'
Wc_c="`wc -c < 'Makefile'`"
test 143 -eq "$Wc_c" ||
echo 'Makefile: original size 143, current size' "$Wc_c"
fi
exit 0
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads